python – 如何获得pandas数据帧中行的百分位数?
Example DataFrame Values -
0 78
1 38
2 42
3 48
4 31
5 89
6 94
7 102
8 122
9 122
stats.percentileofscore(temp['INCOME'].values, 38, kind='mean')
15.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='strict')
10.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='weak')
20.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='rank')
20.0
temp['INCOME'].rank(pct=True)
1 0.20 (Only showing the 38 value index)
temp['INCOME'].quantile(0.11)
37.93
temp['INCOME'].quantile(0.12)
38.31999999999999
Based on the results above, you can see none of the methods are consistent
with the pd.quantiles() method.
我需要为数据帧中的每一行(255M行)获取一列的百分位数,但找不到任何返回他们在pd.quantile&中使用的‘linear interpolation’方法的函数/方法. np.percentile.
我尝试了以下方法/功能 –
.rank(pct=True)
此方法仅返回按顺序排列的值,而不是使用我正在寻找的百分位方法.与pd.quantiles不一致
scipy.stats.percentileofscore
这种方法几乎与我正在寻找的方法更接近,但由于某种原因仍与100%的“线性插值”方法不一致. Related question to this problem with no real answer
我查看了与此问题相关的每个SO答案,但没有一个使用我需要使用的相同插值方法,所以请不要将其标记为重复,除非您可以验证它们使用相同的方法.
在这一点上,我的最后一个选择是找到所有100百分位数的bin截止值并以这种方式应用它或者自己计算线性插值但这看起来非常低效并且将永远适用于255M记录.
还有其他建议吗?
谢谢!