Reputation: 1637
I have the following dataframe with percentiles and I would like to assign values between 0-9 to their respective percentiles.
This is the input:
a
1 0.3038
2 0.0572
3 0.1153
4 0.2168
5 0.5688
6 0.6291
7 0.9416
8 0.7438
9 0.2242
10 0.5662
And this is the desired output:
a b
1 0.3038 3
2 0.0572 0
3 0.1153 1
4 0.2168 2
5 0.5688 5
6 0.6291 6
7 0.9416 9
8 0.7438 7
9 0.2242 2
10 0.5662 5
Also, if the value in column 'a' is 1.00 I would like to return the value 9. For example:
a b
1 1.0000 9
Thank you for your help.
Upvotes: 1
Views: 121
Reputation: 6543
Updated solution
Thanks to @Peter Leimbigler for the following suggestion in the comments, which makes sure that the requirement that 1.0 maps to 9 is correctly handled:
np.floor(df['a']*10).replace({10:9})
Alternatively, sticking with the clip_upper()
idea which was present in the previous solution:
np.floor(df.clip_upper(0.9) * 10).astype(int)
0.9
in the above solution can actually be any number [0.9, 1)
, and will work as intended. The problem in the previous solution is that 1
was used here instead, which meant that a value of exactly 1.0
was not rounded down.
I will leave the original solution below since it was accepted by OP, but as pointed out by @Peter Leimbigler, it did not correctly handle the 1.0 -> 9 special case.
Previous solution
If I understand correctly:
df['b'] = np.floor(df.clip_upper(1) * 10).astype(int)
Gives the same result as in the question and accounts for your caveat regarding a number that is exactly 1.
a b
1 0.3038 3
2 0.0572 0
3 0.1153 1
4 0.2168 2
5 0.5688 5
6 0.6291 6
7 0.9416 9
8 0.7438 7
9 0.2242 2
10 0.5662 5
Upvotes: 3