Issue
I would like to remap a Python Pandas Series of ints to 0,1,2,3,4,... based on their order from smallest to largest. Equal ints should be mapped to the same int.
For example, if I have a pandas Series [1, 1, 4, 4, 7, 12, 18, 18]
, I would like it mapped to [0, 0, 1, 1, 2, 3, 4, 4]
. Basically it's like squishing the ints so that they're next to each other.
I've tried converting to a standard list and using a naive implementation, but wondering if there's a more idiomatic way to do it.
Solution
You can use pd.Categorical
:
s = pd.Series([1, 1, 4, 4, 7, 12, 18, 18])
print(pd.Categorical(s).codes)
Prints:
[0 0 1 1 2 3 4 4]
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.