Pandas dataframe: how to apply describe() to each group and add to new columns?

前端 未结 6 688
执念已碎
执念已碎 2020-12-15 06:20

df:

name score
A      1
A      2
A      3
A      4
A      5
B      2
B      4
B      6 
B      8

Want to get the following new dataframe in

6条回答
  •  星月不相逢
    2020-12-15 06:25

    there is even a shorter one :)

    print df.groupby('name').describe().unstack(1)
    

    Nothing beats one-liner:

    In [145]:

    print df.groupby('name').describe().reset_index().pivot(index='name', values='score', columns='level_1')

提交回复
热议问题