对Pandas DataFrame描述输出进行排序

时间:2019-05-06 07:39:12

标签: python-3.x

我正在尝试对describe()的输出进行计数。不确定如何解决。

尝试了sort_by.loc,但是它们都没有用于对describe的输出进行排序。

需要编辑以下代码行:

df.groupby("Disease_Category")['Approved_Amt'].describe().reset_index()

电流输出

Disease_Category count mean std min 25% 50% 75% max
1 Disease1 5.0 82477.600000 51744.487632 30461.0 58318.00 72201.0 83408.00 168000.0 

2 Disease2 190.0 35357.163158 46268.552683 1707.0 13996.25 22186.0 36281.75 331835.0 

所需的输出

Disease_Category count mean std min 25% 50% 75% max
1 Disease2 190.0 35357.163158 46268.552683 1707.0 13996.25 22186.0 36281.75 331835.0 

2 Disease1 5.0 82477.600000 51744.487632 30461.0 58318.00 72201.0 83408.00 168000.0

2 个答案:

答案 0 :(得分:0)

这应该有效。

import pandas as pd

df = pd.DataFrame({'Disease' : ['Disease1', 'Disease2'],
                   'Category' : [5,190],
                   'count' : [82477, 35357],
                   'mean' : [51744, 46268],
                   'std' : [30461, 1707],
                   'etc' : [1,2]})

print(df)
#   Category   Disease  count  etc   mean    std
#0         5  Disease1  82477    1  51744  30461
#1       190  Disease2  35357    2  46268   1707

# invert rows of dataframe so last row becomes the first
df = df.reindex(index = df.index[::-1])

df = df.reset_index()

#   index  Category   Disease  count  etc   mean    std
#0      1       190  Disease2  35357    2  46268   1707
#1      0         5  Disease1  82477    1  51744  30461

答案 1 :(得分:0)

使用sort_values()Documentation

import pandas as pd

df1 = df.groupby("Disease_Category")['Approved_Amt'].describe().reset_index()
>>>df1
  Disease_Category  count   mean    std    min       25%    50%       75%     max
0         Disease1      5  82477  51744  30461  58318.00  72201  83408.00  168000
1         Disease2    190  35357  46268   1707  13996.25  22186  36281.75  331835

>>>df1.sort_values('count', ascending=False)
  Disease_Category  count   mean    std    min       25%    50%       75%     max
1         Disease2    190  35357  46268   1707  13996.25  22186  36281.75  331835
0         Disease1      5  82477  51744  30461  58318.00  72201  83408.00  168000