简体   繁体   中英

Merge two rows based on a column in Pandas

I have a pandas dataFrame like this:

   version    ids
0  001        {'id': '121'}
1  001        {'id': '122'}
2  002        {'id': '123'}

How do I convert every value of ids to a list and also combine them based on the same version ? Here is what I would like:

   version    ids
0  001        [{'id': '121'}, {'id': '122'}]
1  002        [{'id': '123'}]

I've tried group by:

df.groupby(['version'])['ids'].apply(','.join).reset_index()

but it didn't turn out to a "list", What's the right way to do this? Thanks in advance!!!

Try with

df.groupby(['version'])['ids'].agg(list).reset_index()

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