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

前端 未结 6 696
执念已碎
执念已碎 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:44

    Table is stored in dataframe named df

    df= pd.read_csv(io.StringIO(data),delimiter='\s+')
    

    Just specify column name and describe give you required output. In this way you calculate w.r.t any column

    df.groupby('name')['score'].describe()
    

提交回复
热议问题