Mas*_*dou 3 python dictionary key filter pandas
具有包含字典列的数据框
d = {'p1':[{'Apple':10},{'Ball': 20, 'Cat': 30}]}
df = pd.DataFrame(data=d)
Run Code Online (Sandbox Code Playgroud)
p1
0 {'Apple': 10}
1 {'Ball': 20, 'Cat': 30}
Run Code Online (Sandbox Code Playgroud)
我想过滤键“ Ball”所在的行。
p1
1 {'Ball': 20, 'Cat': 30}
Run Code Online (Sandbox Code Playgroud)
boolean indexing与with in语句一起使用:
df = df[df['p1'].map(lambda x: 'Ball' in x)]
print (df)
p1
1 {'Ball': 20, 'Cat': 30}
Run Code Online (Sandbox Code Playgroud)