python中的百分位

时间:2017-04-05 09:18:20

标签: python python-2.7 pandas dataframe percentile

我试图找到数据集中每个数据的百分位数。我可以输入所需的百分位数并收到价格,但我希望能够输入价格并获得百分位数并将其扩展到我的整个数据集。

                  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

1 个答案:

答案 0 :(得分: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