简体   繁体   中英

Combine multiple rows to a single line in pandas data frame

I have a dataframe in pandas that gives you the below table: 在此处输入图像描述

I would like to get the below results by summing up the calls and getting the type in one single line. (you can observe the yellow rows) 在此处输入图像描述

Try:

# aggregate by Key
# keep the first No, join types, and sum calls
df.groupby("Key", as_index=False).agg({"No": "first", "Type": "; ".join, "Calls": "sum"})

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