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

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

    import pandas as pd
    import io
    import numpy as np
    
    data = """
    name score
    A      1
    A      2
    A      3
    A      4
    A      5
    B      2
    B      4
    B      6
    B      8
        """
    
    df = pd.read_csv(io.StringIO(data), delimiter='\s+')
    
    df2 = df.groupby('name').describe().reset_index().T.drop('name')
    arr = np.array(df2).reshape((4,8))
    
    df2 = pd.DataFrame(arr[1:], index=['name','A','B'])
    
    print(df2)
    

    That will give you df2 as:

                  0     1        2    3    4    5    6    7
        name  count  mean      std  min  25%  50%  75%  max
        A         5     3  1.58114    1    2    3    4    5
        B         4     5  2.58199    2  3.5    5  6.5    8
    

提交回复
热议问题