Dominic Grasso
Dominic Grasso

Reputation: 71

Ungrouping a pandas dataframe after aggregation operation

I have used the groupby() method on my dataframe to find the total number of people at each location. My dataframe's columns are "location" (city names), "name" (names of people in the city), and a "people" column (which just has a "1" for each entry).

dataframe.groupby(by=['location'], as_index=False)['people'].agg('sum')

To the right of the "sum" column, I need to add a column that lists all of the people's names at each location (ideally in separate rows, but a list would be fine too).

Is there a way to "ungroup" my dataframe again after having found the sum?

Upvotes: 1

Views: 2719

Answers (2)

user7864386
user7864386

Reputation:

You can do two different things:

(1) Create an aggregate DataFrame using groupby.agg and calling appropriate methods. The code below lists all names corresponding to a location:

out = dataframe.groupby(by=['location'], as_index=False).agg({'people':'sum', 'name':list})

(2) Use groupby.transform to add a new column to dataframe that has the sum of people by location in each row:

dataframe['sum'] = dataframe.groupby(by=['location'])['people'].transform('sum')

Upvotes: 2

Daniel Weigel
Daniel Weigel

Reputation: 1137

I think you are looking for 'transform' ?

dataframe.groupby(by=['location'], as_index=False)['people'].transform('sum')

Upvotes: 1

Related Questions