Having a dataframe that has a dictionary column
d = {'p1':[{'Apple':10},{'Ball': 20, 'Cat': 30}]}
df = pd.DataFrame(data=d)
p1
0 {'Apple': 10}
1 {'Ball': 20, 'Cat': 30}
I would like to filter rows where the key 'Ball' exists.
p1
1 {'Ball': 20, 'Cat': 30}
Use boolean indexing
with in
statement:
df = df[df['p1'].map(lambda x: 'Ball' in x)]
print (df)
p1
1 {'Ball': 20, 'Cat': 30}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With