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    min25%    50%       75%     max0         Disease1      582477517443046158318.007220183408.001680001         Disease2    1903535746268170713996.252218636281.75331835

>>>df1.sort_values('count', ascending=False)
  Disease_Category  count   mean    std    min25%    50%       75%     max1         Disease2    1903535746268170713996.252218636281.753318350         Disease1      582477517443046158318.007220183408.00168000

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 firstdf = 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"