pandas describe by - additional parameters

后端 未结 3 775
既然无缘
既然无缘 2021-02-06 11:20

I see that the pandas library has a Describe by function which returns some useful statistics. However, is there a way to add additional rows to the output such as

3条回答
  •  时光取名叫无心
    2021-02-06 11:39

    Try this:

     df.describe()
    
          num1  num2
    count   3.0   3.0
    mean    2.0   5.0
    std     1.0   1.0
    min     1.0   4.0
    25%     1.5   4.5
    50%     2.0   5.0
    75%     2.5   5.5
    max     3.0   6.0
    

    Build a second DataFrame.

     pd.DataFrame(df.mad() , columns = ["Mad"] ).T
    
             num1      num2
    Mad  0.666667  0.666667
    

    Join the two DataFrames.

     pd.concat([df.describe(),pd.DataFrame(df.mad() , columns = ["Mad"] ).T ])
    
              num1      num2
    count  3.000000  3.000000
    mean   2.000000  5.000000
    std    1.000000  1.000000
    min    1.000000  4.000000
    25%    1.500000  4.500000
    50%    2.000000  5.000000
    75%    2.500000  5.500000
    max    3.000000  6.000000
    Mad    0.666667  0.666667
    

提交回复
热议问题