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 |
添加回答
舉報