I have a pandas dataframe df which looks as follows. df.to_dict() is given at the end of the question.
Values
0 0.010545
1 0.018079
2 0.019491
3 0.042556
4 0.062404
5 0.077826
6 0.080170
7 0.085732
8 0.097538
9 0.104020
10 0.116825
11 0.121143
12 0.147592
13 0.147939
14 0.154998
15 0.157179
16 0.185593
17 0.200474
Since I have 18 values, I want to classify them into 3 different bins as "Low", "Medium" and "High". I also want to know what is the threshold for each of these bins.
I did
df.apply(lambda x:pd.cut(x, bins = 3, labels=['low','medium','high']), axis = 0)
and got the following.
Values
0 low
1 low
2 low
3 low
4 low
5 medium
6 medium
7 medium
8 medium
9 medium
10 medium
11 medium
12 high
13 high
14 high
15 high
16 high
17 high
I got 5 values for low, 7 for medium and and 6 for high. I am curious why I did not get 6 values for each of low, medium and high since I have 18 values which is divisible by 3.
I also tried to calculate the 33rd and 67th percentile.
df.quantile(0.33) gave me 0.079256 and df.quantile(0.67) gave me 0.131458.
I realized that this is not the exact threshold for low-medium or medium-high for the function that I applied. Because the value in index 5 which is 0.077826 is lower than 33rd percentile (0.079256), but is categorized as "medium".
Is my way of calculating percentiles correct? Does binning also apply the threshold in the same manner? How do I get the threshold for low-medium and medium-high when I applied pd.cut(bins = 3,...) function above?
Is it possible to classify the dataset into equal numbers of low, medium and high based on [0, 0.33, 0.67, 1] as percentiles? Initially, I thought that my function did the same, but it does not look like that.
df.to_dict() is as shown:
{'Values': {0: 0.0105451195503243,
1: 0.01807949818715662,
2: 0.01949062427056047,
3: 0.04255627128922379,
4: 0.06240376897660298,
5: 0.07782590379116708,
6: 0.0801695217422988,
7: 0.0857317068170362,
8: 0.0975380806573516,
9: 0.1040201601240209,
10: 0.1168250536954563,
11: 0.1211426350058809,
12: 0.1475922708843568,
13: 0.1479393893305906,
14: 0.1549975110559438,
15: 0.157178926862648,
16: 0.1855926516856752,
17: 0.2004743800065415}}