I have a pandas DataFrame with a structure as follows:
data = DataFrame({'Cat1':['A', 'B', 'B', 'C'], 'Cat2': ['X', 'Y', 'Z', 'X'], 'Counter': [0, 4, 1, 5]})
Now I want to add a separate column with a ranking by Cat1 (so in this case: 1,3,2,4 as new column). My first try was:
data['ranking'] = data['ranking'] + data[data['Cat1'] == 'A']['Counter'].rank(ascending=0).fillna(0)
However, when I add the second Category (data['Cat1']=='B' as condition), it overrides the existing values. This is what I expected, as I have to use .add() as far as I understand. However, the same happens with the following script:
data['ranking'].add(data[data['Cat1']=='A']['Counter'].rank(ascending=0))
Also overrides all values where Cat1==B with NA. How can I avoid this?
Thanks in advance!
-----------------------EDIT!!------------------
Let's say this is my table:

And ordinary rank would give me a ranking of all numbers 1 through 12. Now what I need is a ranking based on the category and as an additional column in the original python DataFrame.
Hence, the last column should look say: 2 (second-ranked value of a) 3 (third-ranked value of a) 1 (first-ranked value of a) 1 (first-ranked value of b) 1 (first-ranked value of c) 5 2 ...