This is similar to LabelEncoder from scikit-learn, but with the requirement that the number value assignments occur in order of frequency of the category, i.e., the higher occurring category being assigned the highest/lowest (depending on use-case) number.
E.g. If the variable can take values [a, b, c] with frequencies such as
Category
0 a
0 a
0 a
0 a
0 a
1 b
1 b
1 b
1 b
1 b
1 b
1 b
1 b
1 b
1 b
2 c
2 c
a occurs 5 times, b occurs 10 times and c occurs 2 times.
Then I want the replacements be done as b=1, a=2 and c=3.