Reputation: 2024
I have a pandas Series containing a list of dictionaries. I'd like to filter out dictionaries based on a condition. Here's some sample data:
import pandas as pd
df = pd.DataFrame({'d': [[{'br': 1, 'ba': 1, 'r': 100},
{'ba': 1, 'r': 80},
{'br': 2, 'ba': 1, 'r': 150},
{'br': 1, 'ba': 1, 'r': 90}],
[{'br': 1, 'ba': 1, 'r': 100},
{'ba': 1, 'r': 80},
{'br': 2, 'ba': 1, 'r': 150}]],
'id': ['xxas', 'yxas'],
'name': ['A', 'B']
})
I'd like to remove all the dicts
with value
of key r
> 100
. Expected output:
dff = pd.DataFrame({'d': [[{'br': 1, 'ba': 1, 'r': 100},
{'ba': 1, 'r': 80},
{'br': 1, 'ba': 1, 'r': 90}],
[{'br': 1, 'ba': 1, 'r': 100},
{'ba': 1, 'r': 80}]],
'id': ['xxas', 'yxas'],
'name': ['A', 'B']
})
Upvotes: 0
Views: 84
Reputation:
You could use a list comprehension to filter:
df['d'] = [[d for d in row if d['r']<=100] for row in df['d']]
Output:
d id name
0 [{'br': 1, 'ba': 1, 'r': 100}, {'ba': 1, 'r': ... xxas A
1 [{'br': 1, 'ba': 1, 'r': 100}, {'ba': 1, 'r': ... yxas B
Upvotes: 2