1

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 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.