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