2 回答

TA贡献1966条经验 获得超4个赞
我对 Excel 的 Percentrank 函数不是很熟悉,但看起来您可以使用以下方法获得相同的结果:
def percent_rank(pd_series, value, precision): return np.round((pd_series < value).astype(int).sum()/(len(pd_series) -1), precision)
如果您有兴趣一次获取所有值(即每个值在您的范围内的位置):
def percent_rank(pd_series, precision): return [np.round((pd_series< value).astype(int).sum()/(len(pd_series) -1), precision) for value in pd_series]
希望有帮助!

TA贡献1831条经验 获得超4个赞
这是一个处理原始数组中不存在的重复项和值的版本:
def percent_rank(arr, score, sig_digits=8):
arr = np.asarray(arr)
arr = np.round(arr, sig_digits)
score = np.round(score, sig_digits)
if score in arr:
small = (arr < score).sum()
return small / (len(arr) - 1)
else:
if score < arr.min():
return 0
elif score > arr.max():
return 1
else:
arr = np.sort(arr)
position = np.searchsorted(arr, score)
small = arr[position - 1]
large = arr[position]
small_rank = ((arr < score).sum() - 1) / (len(arr) - 1)
large_rank = ((arr < large).sum()) / (len(arr) - 1)
step = (score - small) / (large - small)
rank = small_rank + step * (large_rank - small_rank)
return rank
Excel 文档中的示例:
公式 | 描述 | 结果 |
---|---|---|
=PERCENTRANK.INC(A2:A11,2) | 2 在 A2:A11 范围内的百分比等级(0.333,因为集合中的 3 个值小于 2,6 个大于 2;3/(3+6)=0.333)。 | 0.333 |
=PERCENTRANK.INC(A2:A11,4) | A2:A11 范围内 4 的百分比等级。 | 0.555 |
=PERCENTRANK.INC(A2:A11,8) | A2:A11 范围内 8 的百分比排名 | 0.666 |
=PERCENTRANK.INC(A2:A11,5) | 在 A2:A11 范围内排名 5 的百分比(0.583,介于 4 的 PERCENTRANK.INC 和 8 的 PERCENTRANK.INC 之间的四分之一)。 | 0.583 |
与函数的输出相匹配
分数 | 公式 | 结果 |
---|---|---|
2 | 百分比排名(arr,2) | 0.333 |
4 | 百分比排名(arr,4) | 0.556 |
8 | 百分比排名(arr,8) | 0.667 |
5 | 百分比排名(arr,5) | 0.583 |
添加回答
举报