2

I am trying to find the percentile of each data inside a dataset. I can input the desired percentile and receive the price, but I want to be able to input the price and receive the percentile and extend that to my entire dataset.

                  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
Connor
  • 13
  • 4
user6457870
  • 247
  • 5
  • 14
  • 2
    Have you tried [`qcut`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.qcut.html)? – IanS Apr 05 '17 at 09:19
  • 2
    very first link when you google for `percentile in python` http://stackoverflow.com/questions/2374640/how-do-i-calculate-percentiles-with-python-numpy – Surajano Apr 05 '17 at 09:21
  • Thank you very much for both of your comment. @Surajano I am trying to do the opposite of that actually. qcut send me back something bizare.. – user6457870 Apr 05 '17 at 09:28
  • 1
    See duplicate: the trick is to use `labels=False` in `qcut` (I learned something today). – IanS Apr 05 '17 at 09:43

1 Answers1

2

You want pd.Series.rank with pct=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
piRSquared
  • 285,575
  • 57
  • 475
  • 624