Skip to content
Advertisement

How to create rank column in Python based on other columns

I have a python dataframe that looks like the following:

enter image description here

This dataframe has been sorted in descending order by 'transaction_count'. I want to create another column in that dataframe called 'rank' that contains the count of occurrences of cust_ID. My desired output would look something like the following:

enter image description here

For cust_ID = 1234 with transaction_count = 4, the rank would be 1, for the next appearance of cust_ID = 1234, the rank would be 2 and so on.

I tried the following among other things:

JavaScript

But the rank column gets created as all NaN values

enter image description here

Any suggestions on how to approach this would be greatly appreciated!

Advertisement

Answer

Use groupby + cumcount:

JavaScript

Output

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