How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

Viewed 176

I have a DataFrame where one column/field is a long list of dictionaries. I want to only keep rows in a subset of the DataFrame where the list of dictionaries contains a certain dictionary entry. I don't want to filter the list of dictionaries, just retrieve rows where the desired entry exists (usually among many other entries) keeping all other columns/fields intact.

Here's a mock df

df = pd.DataFrame({'bird': ['robin', 'jay', 'pelican', 'duck'], 'beaky': ['yes', 'yes', 'yes', 'yes'], 'feathers': [[{'type':'thing', 'id':'1a'}, {'type':'thing', 'id':'5a'}] , [{'type': 'thing', 'id':'2a'},{'type':'thing', 'id':'1a'}],[{'type': 'thing', 'id':'3a'},{'type': 'thing', 'id':'4a'}],[{'type':'thing', 'id':'2a'}, {'type':'thing', 'id':'3a'}]]})

df

Pseudo code for the df example above...

Select rows of the DataFrame where df['feathers'] contains {'type': 'thing', 'id': '3a'}

1 Answers

Convert to string then str.contains

m=df.feathers.astype(str).str.contains("{'type': 'thing', 'id': '3a'}")
0    False
1    False
2     True
3     True
Name: feathers, dtype: bool
df=df[m]
Related