Skip to content Skip to sidebar Skip to footer

Pandas Dataframe: Groupby One Column, But Concatenate And Aggregate By Others

How do I turn the below input data (Pandas dataframe fed from Excel file): ID Category Speaker Price 334014 Real Estate Perspectives Tom Smith 100

Solution 1:

There are 2 possible solutions - aggregate by multiple columns and join:

dataframe.groupby(['ID','Category','Price'])['Speaker'].apply(','.join)

Or need aggregate only Price column, then is necessary aggregate all columns by first or last:

dataframe.groupby('Price').agg({'Speaker':','.join, 'ID':'first', 'Price':'first'})

Solution 2:

Post a Comment for "Pandas Dataframe: Groupby One Column, But Concatenate And Aggregate By Others"