我有以下数据框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功能可以执行上述任务?谢谢!
使用 groupby
pd.concat
g = df.groupby('team').member pd.concat([g.apply(list), g.count()], axis=1, keys=['members', 'number'])
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