简体   繁体   English

如何选择要在熊猫分组显示的列值

[英]how to select column values to display in pandas groupby

I am using pandas 0.16.0, I have data: 我正在使用熊猫0.16.0,我有数据:

id A  B
1  10 100
2  10 101
3  20 102

when I call 当我打电话

df.groupby(['A']).groups

I have 我有

{10: [1 2], 20: [3]}

and I want to have this (values from column B) 我想要这个(B列中的值)

{10: [100, 101], 20: [102]}

please help 请帮忙

One way is to groupby and apply function to take list, and then convert to dict. 一种方法是对groupby进行分组并将其应用到列表中,然后转换为dict。

In [92]: df.groupby(['A']).apply(lambda x: x['B'].tolist()).to_dict()
Out[92]: {10: [100, 101], 20: [102]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM