How converting a pandas groupby/describe to standard dataframe?

Viewed 112

By doing this command on my dataframe I get the following dataframe:

label = dataframe[['label', 'area']].groupby(['label']).describe()
print(label)
           area                                                                 
          count        mean         std    min     25%     50%      75%      max
label                                                                           
square    379.0  546.057942  728.552019  67.29  249.24  373.37  569.025  9772.17
triangle  179.0  439.527765  363.232426  66.61  259.03  360.22  496.545  3702.97

I would like simply this result:

  label     count        mean         std    min     25%     50%      75%      max                     
0 square    379.0  546.057942  728.552019  67.29  249.24  373.37  569.025  9772.17
1 triangle  179.0  439.527765  363.232426  66.61  259.03  360.22  496.545  3702.97

What can I do?

Also how to do the same thing if I apply to label the method .unstack(1)? To have another point of view. Thanks !

1 Answers

You can specify column after groupby:

label = dataframe.groupby('label')['area'].describe()

In your solution remove first level of MultiIndex:

label = dataframe[['label', 'area']].groupby(['label']).describe().droplevel(0, axis=1)
Related