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.
我需要获取数据帧(2.55 亿行)中每一行的一列的百分位数,但找不到返回 'linear interpolation' 的任何函数/方法。他们在 pd.quantile
和 np.percentile
中使用的方法。
我尝试了以下方法/功能 -
.rank(pct=True)
此方法只返回按顺序排列的值,而不是使用我正在寻找的百分位数方法。与 pd.quantiles
scipy.stats.percentileofscore
这种方法几乎更接近我正在寻找的方法,但由于某种原因仍然不是 100% 与“线性插值”方法一致。 Related question to this problem with no real answer
我已经查看了与此问题相关的每个 SO 答案,但它们都没有使用我需要使用的相同插值方法,因此请不要将其标记为重复,除非您可以验证它们使用的是相同的方法。
在这一点上,我的最后一个选择是只找到所有 100 个百分位数的 bin 截止值并以此方式应用它或自己计算线性插值,但这似乎效率很低,并且将永远应用于 255M 记录。
还有其他建议吗?
谢谢!
最佳答案
TL;博士
使用
sz = temp['INCOME'].size-1
temp['PCNT_LIN'] = temp['INCOME'].rank(method='max').apply(lambda x: 100.0*(x-1)/sz)
INCOME PCNT_LIN
0 78 44.444444
1 38 11.111111
2 42 22.222222
3 48 33.333333
4 31 0.000000
5 89 55.555556
6 94 66.666667
7 102 77.777778
8 122 100.000000
9 122 100.000000
回答
其实很简单,一旦你理解了机制。当您寻找分数的百分位数时,您已经在每一行中有了分数。剩下的唯一一步是了解您需要小于或等于所选值的百分位数。这正是 scipy.stats.percentileofscore()
的 kind='weak' 和 DataFrame 的 method='average' 的参数.rank()
做。为了反转它,使用 interpolation='lower' 运行 Series.quantile()
。
因此,scipy.stats.percentileofscore()
、Series.rank()
和 Series.quantile()
的行为 < strong>是一致的,见下文:
In[]:
temp = pd.DataFrame([ 78, 38, 42, 48, 31, 89, 94, 102, 122, 122], columns=['INCOME'])
temp['PCNT_RANK']=temp['INCOME'].rank(method='max', pct=True)
temp['POF'] = temp['INCOME'].apply(lambda x: scipy.stats.percentileofscore(temp['INCOME'], x, kind='weak'))
temp['QUANTILE_VALUE'] = temp['PCNT_RANK'].apply(lambda x: temp['INCOME'].quantile(x, 'lower'))
temp['RANK']=temp['INCOME'].rank(method='max')
sz = temp['RANK'].size - 1
temp['PCNT_LIN'] = temp['RANK'].apply(lambda x: (x-1)/sz)
temp['CHK'] = temp['PCNT_LIN'].apply(lambda x: temp['INCOME'].quantile(x))
temp
Out[]:
INCOME PCNT_RANK POF QUANTILE_VALUE RANK PCNT_LIN CHK
0 78 0.5 50.0 78 5.0 0.444444 78.0
1 38 0.2 20.0 38 2.0 0.111111 38.0
2 42 0.3 30.0 42 3.0 0.222222 42.0
3 48 0.4 40.0 48 4.0 0.333333 48.0
4 31 0.1 10.0 31 1.0 0.000000 31.0
5 89 0.6 60.0 89 6.0 0.555556 89.0
6 94 0.7 70.0 94 7.0 0.666667 94.0
7 102 0.8 80.0 102 8.0 0.777778 102.0
8 122 1.0 100.0 122 10.0 1.000000 122.0
9 122 1.0 100.0 122 10.0 1.000000 122.0
现在,在 PCNT_RANK
列中,您会得到小于或等于 INCOME
列中值的比率。但如果您想要“插值”比率,它位于 PCNT_LIN
列中。当您使用 Series.rank()
进行计算时,它非常快,可以在几秒钟内计算出 2.55 亿个数字。
在这里,我将解释如何通过使用 quantile()
和 linear
插值来获取值:
temp['INCOME'].quantile(0.11)
37.93
我们的数据 temp['INCOME']
只有十个值。根据你的公式 link to Wiki第 11 个百分位数的排名是
rank = 11*(10-1)/100 + 1 = 1.99
rank的截断部分为1,对应值为31,rank为2(即下一个bin)的值为38。fraction的值
是排名 的小数部分。这导致结果:
31 + (38-31)*(0.99) = 37.93
对于值本身,fraction
部分必须为零,因此很容易进行逆计算得到百分位数:
p = (rank - 1)*100/(10 - 1)
我希望我说得更清楚。
关于python - 如何获取 Pandas 数据框中一行的百分位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804120/