Searching for strings in lists inside Pandas DataFrame

  indexing, list, pandas, python

I’m trying to search for strings within lists that are contained in a pandas dataframe, see this one example:

       userAuthor     hashtagsMessage
post_1    nytimes            [#Emmys]
post_2        TMZ                  []
post_3     Forbes        [#BTSatUNGA]
post_4    nytimes            [#Emmys]
post_5     Forbes  [#BTS, #BTSatUNGA]

As you have noticed, the column that hosts such lists is ‘hashtagsMessage’. I’ve tried using conventional methods for string searching but I’ve not been able to.

If I wanted to look for ‘#BTS’, with a conventional method, you could use some of these options, like:

df['hashtagsMessage'].str.contains("#BTS", case=False)

or

df['hashtagsMessage']=="#BTS" 

Or similar. Unfortunately, these approaches do not work for lists, I have to make an extra step I suppose to index inside the list while I’m searching in the DataFrame but I’m not really sure how to do this part.

Any help is entirely appreciated!

Source: Python Questions

LEAVE A COMMENT