2017-04-05 93 views
1

我正在嘗試查找數據集內每個數據的百分位數。我可以輸入所需的百分比並接收價格,但我希望能夠輸入價格並接收百分位並將其延伸到我的整個數據集。python中的百分位數

    Price Percentile 
2018-01-31  121 
2018-04-30  12.8 
2018-07-30  141 
2018-10-31  90 
2019-01-31  120 
2019-04-30  51.194 
+2

你試過['qcut'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.qcut.html )? – IanS

+2

非常第一個鏈接,當你谷歌的'百分點python' http://stackoverflow.com/questions/2374640/how-do-i-calculate-percentiles-with-python-numpy – Surajano

+0

非常感謝你的兩個你的評論。 @Surajano我試圖做相反的事實。 qcut發回我的東西bizare .. – user6457870

回答

2

你想pd.Series.rankpct=True

df.assign(Percentile=df.Price.rank(pct=True)) 

       Price Percentile 
2018-01-31 121.000 0.833333 
2018-04-30 12.800 0.166667 
2018-07-30 141.000 1.000000 
2018-10-31 90.000 0.500000 
2019-01-31 120.000 0.666667 
2019-04-30 51.194 0.333333