简体   繁体   中英

Select rows from dataframe where column values are lists of strings

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM