嗨,我想比较熊猫数据帧的相同键
数据框
汽车 | values(dict) | |
---|---|---|
0 | 奥迪 | {'colour': 'black', 'PS': '230', 'owner': 'peter'} |
1 | 奥迪 | {'owner': 'fred', 'colour': 'black', 'PS': '230', 'number': '155555'} |
2 | 福特 | {'windows': 'yes', 'PS': '230', 'owner': 'pam'} |
3 | 宝马 | {'colour': 'black', 'windows': 'yes', 'owner': 'peter', 'doors': '5'} |
颜色 | owner | PS | 数字 | 窗口 | 门 | |
---|---|---|---|---|---|---|
奥迪 | 2 | 2 | 2 | 1 | 0 | 0 |
福特 | 0 | 1 | 1 | 0 | 1 | 0 |
宝马 | 1 | 1 | 0 | 0 | 1 | 1 |
有什么有效的办法
答案 0 :(得分:0)
apply(pd.Series)
将 dict 扩展为列join()
这回到没有 dict 列的原始 DFgroupby().count()
即可获得您想要的结果df.loc[:,"car"].to_frame().join(df["values(dict)"].apply(pd.Series)).groupby("car",as_index=False).count()
汽车 | 颜色 | PS | owner | 数字 | windows | 门 | |
---|---|---|---|---|---|---|---|
0 | 奥迪 | 2 | 2 | 2 | 1 | 0 | 0 |
1 | 宝马 | 1 | 0 | 1 | 0 | 1 | 1 |
2 | 福特 | 0 | 1 | 1 | 0 | 1 | 0 |