简体   繁体   中英

How to merge cells into a single one with Pandas?

数据框的图片

I'm trying to 'merge' the item section into a list according to the Transaction ID. So, I want Chocolate Quente and Jam on a list.

Can you try the following: item_list=df[df["Transaction"]==3]["Item"].to_list() or list(df[df["Transaction"]==3]["Item"]

I suppose this should work if 3 is an int type. If it's an object (string), use double quotes "3".

df.groupby('Transaction')['Item'].apply(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