简体   繁体   中英

how to select column values to display in pandas groupby

I am using pandas 0.16.0, I have data:

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)

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

please help

One way is to groupby and apply function to take list, and then convert to dict.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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