Calculate Percentile For Every Value In A Column Of Dataframe
I am trying to calculate percentile for every value in column a from a DataFrame x. Is there a better way to write the following piece of code? x['pcta'] = [stats.percentileofscore
Solution 1:
It seems like you want Series.rank()
:
x.loc[:, 'pcta'] = x.rank(pct=True) # will be indecimal form
Performance:
import scipy.stats as scs
%timeit [scs.percentileofscore(x["a"].values, i) for i in x["a"].values]
1000 loops, best of3: 877 µs per loop
%timeit x.rank(pct=True)
10000 loops, best of3: 107 µs per loop
Post a Comment for "Calculate Percentile For Every Value In A Column Of Dataframe"