Friday, October 7, 2022

[FIXED] How to find the quantile value of each number in pandas

Issue

I have the following code which produces df with random values:

import pandas as pd
import numpy as np
random_data = np.random.randint(10, 25, size=20)
df = pd.DataFrame(random_data, columns=['RANDOM VALUES'])
df.index.name = 'foo'
print(df)

This will produce:

     RANDOM VALUES
foo               
0               15
1               20
2               17
3               21
4               23
5               20
6               23
7               22
8               22
9               21
10              23
11              17
12              12
13              17
14              17
15              24
16              13
17              20
18              14
19              22

To find the quantile value for say quantile(0.5)

df['RANDOM VALUES'].quantile(0.5)

How can i do it the other way round? Such as create a column with quantile value of each number?


Solution

you can use the percentileofscore function from the scipy.stats module. Refer to the documentation for proper use of the kind argument:

import scipy.stats as stats

df["RANDOM VALUES"].apply(lambda x: stats.percentileofscore(df["RANDOM VALUES"],
                                                             x, kind = 'weak'))


Answered By - Mankind_008
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.