Skip to content Skip to sidebar Skip to footer

Sorting The Pandas DataFrame Describe Output

I am trying to sort the output of describe() with count. Not sure, how to solve. Tried sort_by and .loc but none of it serves the purpose of sorting the output of describe. Need t

Solution 1:

Use 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

Solution 2:

This should work.

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

Post a Comment for "Sorting The Pandas DataFrame Describe Output"