有一个包含字典列的数据框
d = {'p1':[{'Apple':10},{'Ball': 20, 'Cat': 30}]}
df = pd.DataFrame(data=d)
p1
0 {'Apple': 10}
1 {'Ball': 20, 'Cat': 30}
我想过滤存在键“球”的行。
p1
1 {'Ball': 20, 'Cat': 30}
最佳答案
将 boolean indexing
与 in
语句一起使用:
df = df[df['p1'].map(lambda x: 'Ball' in x)]
print (df)
p1
1 {'Ball': 20, 'Cat': 30}
关于Python Pandas - 如何使用键的存在过滤具有包含字典的列的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085059/