如何 select 列值显示在 pandas groupby 中

how to select column values to display in pandas groupby

我正在使用pandas 0.16.0,我有数据:

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

当我打电话时

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

我有

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

我想要这个(B 列中的值)

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

请帮忙

一种方法是groupby和apply function取list,然后转换成dict。

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