Skip to content
Advertisement

In pandas, how to create a new column with a rank according to the mean values of another column

I have the following pandas dataframe

JavaScript

I want to create a new column that ranks each of the countries according to the mean of their values from largest to smallest

The output would look like the following

JavaScript

Note that I don’t need the average column, its just there to help with the explanation.

Many thanks

Advertisement

Answer

Use groupby + transform for mean and then rank:

JavaScript

Similar solution:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement