Search code examples
pythonpandasdataframeranking

Ranking values in pandas based off a condition


I am fairly new to python. I have a dataframe that looks something along these lines:

Points   Year_Month 
5000     March-2021
4000     March-2021
3500     March-2021
4500     February-2021
2000     February-2021
1500     February-2021
6000     January-2021
1200     January-2021
1000     January-2021

I want to create a new column that ranks the 'Points' column based on the 'Year_Month' column. So the output that I want would look something like the following:

Points   Year_Month     Rank  
5000     March-2021     1
4000     March-2021     2
3500     March-2021     3
4500     February-2021  1
2000     February-2021  2
1500     February-2021  3
6000     January-2021   1
1200     January-2021   2
1000     January-2021   3

Any help would be much appreciated. Thanks


Solution

  • use groupby and rank

    df['rank'] = df.groupby(['Year_Month'])['Points'].rank(method='first', ascending=False)
    

    Look at what sort of rank method is suitable by looking at the docs here.