具有具有字典列的数据帧
d = {'p1':[{'Apple':10},{'Ball': 20, 'Cat': 30}]}
df = pd.DataFrame(data=d)
p1
0 {'Apple': 10}
1 {'Ball': 20, 'Cat': 30}
我想筛选键'Ball‘存在的行。
p1
1 {'Ball': 20, 'Cat': 30}
发布于 2019-11-28 09:08:23
在in
语句中使用boolean indexing
:
df = df[df['p1'].map(lambda x: 'Ball' in x)]
print (df)
p1
1 {'Ball': 20, 'Cat': 30}
发布于 2019-11-28 09:04:37
下面这行就可以了
df[['Ball' in x.keys() for x in df.p1]]
发布于 2019-11-28 09:08:01
这也可能对您的理解有所帮助。
d = {'p1':[{'Apple':10},{'Ball': 20, 'Cat': 30}]}
df = pd.DataFrame(data=d)
df2 = df['p1'].apply(pd.Series) # would break the dictionary into different columns
df2
Apple Ball Cat
0 10.0 NaN NaN
1 NaN 20.0 30.0
在这里,您可以直接拾取具有Ball的列。
https://stackoverflow.com/questions/59085059
复制