How to filter column in Data frame based on text containing symbol #

Viewed 24

Hi have a Data frame with two columns which looks like this:

Index  Text

0      READ MY NEW OP-ED: IRREVERSIBLE – Many Effects...
1      #COVID19 is linked to more #diabetes diagnoses...
2      #COVID19: IRREVERSIBLE – Many Effects...
3      READ MY NEW OP-ED: IRREVERSIBLE – Many Effects...
4      Advanced healthcare at your fingertips\nhttps:...

I am trying keep only the rows which contain the #symbol, so based on my data frame above my desired output is:

Index  Text

1      #COVID19 is linked to more #diabetes diagnoses...
2      #COVID19: IRREVERSIBLE – Many Effects...

I have tried several ways to obtain that unsuccessfully, my latest code attempt was:

for column in twt_text:
    print(twt_text['text'].str.contains('#'))

But the output generated was not at all what I expected:

0   False
1   True
2   True
3   False
4   False

Any idea or insight on how I can obtain the output I want based on text containing # ?

1 Answers

You could build a selection mask and use that to filter the rows:

df[df['Text'].str.contains('#')]

Result

                                                Text
1  #COVID19 is linked to more #diabetes diagnoses...
2           #COVID19: IRREVERSIBLE – Many Effects...
Related