简体   繁体   中英

Assigning percentile to each value of pandas series

Is there a direct out-of-the-box way to assign percentile to each of the values of pandas series?

I'm achieving this calculation via ranking and rescaling, like here:

values = pd.Series(np.random.normal(0,1,100))
percentiles = values.rank()/values.shape

for example the Series: [0,2,4,2,10,8,6,1]

should receive the percentiles: [0.125, 0.4375, 0.625, 0.4375, 1.0, 0.875, 0.75, 0.25]

However I'm very surprised that there is no native solution for that (such as qcut , quantile etc...)

Is there is an explicit way to calculate it?

You can do this using rank , where pct=True option displays ranks in percentile form.

In [1551]: v = pd.Series([0,2,4,2,10,8,6,1])
In [1556]: v.rank(pct=True)                 
Out[1556]: 
0    0.1250
1    0.4375
2    0.6250
3    0.4375
4    1.0000
5    0.8750
6    0.7500
7    0.2500
dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM