简体   繁体   English

聚合操作后取消分组 pandas dataframe

[英]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.我在我的 dataframe 上使用了“groupby”方法来查找每个位置的总人数。

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?找到总和后,有没有办法再次“取消组合”我的 dataframe?

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

You can do two different things:你可以做两件不同的事情:

(1) Create an aggregate DataFrame using groupby.agg and calling appropriate methods. (1) 使用groupby.agg并调用适当的方法创建聚合 DataFrame。 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: (2) 使用groupby.transformdataframe添加一个新列,其中每一行都有按位置划分的人数总和:

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

I think you are looking for 'transform'?我认为您正在寻找“转变”?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM