Select rows from dataframe where column values are lists of strings

Viewed 55

I have a dataframe which I want to filter based on a column that contains lists of strings.

example:

df["artists"].head()

0    ['Sergei Rachmaninoff', 'James Levine', 'Berli...
1                                       ['Dennis Day']
2    ['KHP Kridhamardawa Karaton Ngayogyakarta Hadi...
3                                     ['Frank Parker']
4                                       ['Phil Regan']
Name: artists, dtype: object

                 

I would like to do something along the lines of

df[df['artists'] == 'Dennis Day']

However this returns an empty dataframe.

Ive made several other attempts but cant seem to figure out how to filter by lists, and my search results tend to give ways of passing a list into a filter.

Im sure its pretty obvious but any help would be much appreciated.

Thanks

1 Answers

use apply and mask

import pandas as pd
df= pd.DataFrame(columns=["artists"])
df.loc[0,"artists"] =   ['Frank Parker','Dennis Day']
df.loc[1,"artists"] =  ['Sergei Rachmaninoff', 'James Levine']

mask = df.artists.apply(lambda row:'Dennis Day' in row)
df = df[mask]
df
Related