pandas:给定列的聚合行并计算数字

时间:2017-01-11 00:53:44

标签: python python-3.x pandas

我有以下数据框my_df

team      member
--------------------    
 A         Mary
 B         John
 C         Amy
 A         Dan
 B         Dave
 D         Paul
 B         Alex
 A         Mary
 D         Mary

我希望新输出新数据框new_df为:

team      members              number
--------------------------------------
 A       [Mary,Dan]              2
 B       [John,Dave,Alex]        3
 C       [Amy]                   1
 D       [Paul,Mary]             2

我想知道是否有任何现有的pandas功能可以执行上述任务?谢谢!

3 个答案:

答案 0 :(得分:4)

使用private dynamic GetAddressByIncID(int? incID) { var incAddress = // Query here return incAddress; }

groupby

pd.concat

g = df.groupby('team').member pd.concat([g.apply(list), g.count()], axis=1, keys=['members', 'number'])

agg

答案 1 :(得分:3)

另一个选择:

(df.groupby("team", as_index=False).member
   .agg({"member": lambda x: list(x), "count": "count"}))

enter image description here

答案 2 :(得分:1)

使用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)