Home > Enterprise >  Combine multiple rows to a single line in pandas data frame
Combine multiple rows to a single line in pandas data frame

Time:06-16

I have a dataframe in pandas that gives you the below table: enter image description here

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) enter image description here

CodePudding user response:

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"})
  • Related