Given the following example:
example = pd.DataFrame({'y':[1,1,1,1,1,1,1,1,1,1,2,2,2,2,0,0,-1,-1,-1]})
I would like to relabel the values by their frequency counts, in descending order. So I want the value (e.g. 1) with the largest number of cases to be replaced by 0, then the next largest bin with 1, and so on through all of the values. The caveat is I want to ignore the cases that take on values of -1. If I run value_counts(), I can see this:
y
1 10
2 4
-1 3
0 2
dtype: int64
But I would like a pythonic and non-hacky/clean solution to create the following:
y
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 1
11 1
12 1
13 1
14 2
15 2
16 -1
17 -1
18 -1
y
0 10
1 4
-1 3
2 2
dtype: int64
(ideally I retain the old column as well for good record keeping). I could loop over each value, check that it is not -1, then value_counts() and replace it by the iteration number, but this feels quite high maintenance. Is there a clean way to achieve it?