0

I am trying to sort the describe of a dataframe, read several helps posts on very similar matter, but I can no make it work.

My three lines of code go like this:

df1 = myDataset.groupby(['country_code'])['country_code','score'].describe() 

with pd.option_context('display.max_rows', None, 'display.max_columns', None):

display(df1) 
display(df1.reset_index()) 
display(df1.reset_index().sort_values('count', ascending=False))

First display shows :

    score
count   mean    std min 25% 50% 75% max
country_code                                
AE  10.0    10.000000   31.622777   0.0 0.00    0.0 0.00    100.0
AM  1.0 0.000000    NaN 0.0 0.00    0.0 0.00    0.0
AO  5.0 0.000000    0.000000    0.0 0.00    0.0 0.00    0.0
...

second display goes:

    country_code    score
count   mean    std min 25% 50% 75% max
0   AE  10.0    10.000000   31.622777   0.0 0.00    0.0 0.00    100.0
1   AM  1.0 0.000000    NaN 0.0 0.00    0.0 0.00    0.0
2   AO  5.0 0.000000    0.000000    0.0 0.00    0.0 0.00    0.0
...

and the third raise an error:

KeyError                                  Traceback (most recent call last)
<ipython-input-28-855a7c54543c> in <module>()
      6       display( df1.reset_index() )
      7 
----> 8       display( df1.reset_index().sort_values('count', ascending=False) )

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in _get_label_or_level_values(self, key, axis)
   1561             values = self.axes[axis].get_level_values(key)._values
   1562         else:
-> 1563             raise KeyError(key)
   1564 
   1565         # Check for duplicates

KeyError: 'count'

Not sure what I am doing difrerently than other solutions as: Sorting the Pandas DataFrame Describe output

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vswers
  • 11
  • 1
  • Linked solution used only one key for ```.describe()```, but you used two keys ```['country_code','score']```. So ```df1.reset_index()``` have multi-column index and ```.sort_values('count', ascending=False)``` can not find key ```'count'```. Please see https://stackoverflow.com/questions/14733871/multi-index-sorting-in-pandas – Ilkyun Im Apr 06 '21 at 08:10
  • dindnt know about "multi-column index" thanks a lot !! – vswers Apr 06 '21 at 08:43

1 Answers1

1
df1.sort_values([('score','count')], ascending=False)

thanks @ilkyun-im

vswers
  • 11
  • 1