pandas 熊猫:聚合给定列的行并计算数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41581044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:45:43 来源:igfitidea点击:
pandas: aggregate rows for a given column and count the number
提问by Edamame
I have the following data frame my_df
:
我有以下数据框my_df
:
team member
--------------------
A Mary
B John
C Amy
A Dan
B Dave
D Paul
B Alex
A Mary
D Mary
I want the new output the new data frame new_df
as:
我希望新数据框的新输出new_df
为:
team members number
--------------------------------------
A [Mary,Dan] 2
B [John,Dave,Alex] 3
C [Amy] 1
D [Paul,Mary] 2
I am wondering is there any existing pandas function can perform the above task? Thanks!
我想知道是否有任何现有的 Pandas 函数可以执行上述任务?谢谢!
采纳答案by piRSquared
using groupby
使用 groupby
pd.concat
pd.concat
g = df.groupby('team').member
pd.concat([g.apply(list), g.count()], axis=1, keys=['members', 'number'])
agg
agg
g = df.groupby('team').member
g.agg(dict(members=lambda x: list(x), number='count'))
members number
team
A [Mary, Dan] 2
B [John, Dave, Alex] 3
C [Amy] 1
D [Paul] 1
回答by Psidom
回答by racket99
using lambda
:
使用lambda
:
newdf=pd.DataFrame()
newdf['team']=my_df['team'].unique()
newdf['members']=newdf['team'].map(lambda x:list(my_df[my_df['team']==x]['member']))
newdf['number']=newdf.members.map(lambda x: len(x))
newdf.set_index('team',inplace=True)