Calculate percentile for every value in a column of dataframe

前端 未结 1 1456
清歌不尽
清歌不尽 2020-12-19 09:40

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 c

相关标签:
1条回答
  • 2020-12-19 10:03

    It seems like you want Series.rank():

    x.loc[:, 'pcta'] = x.rank(pct=True) # will be in decimal form
    

    Performance:

    import scipy.stats as scs
    
    %timeit [scs.percentileofscore(x["a"].values, i) for i in x["a"].values]
    1000 loops, best of 3: 877 µs per loop
    
    %timeit x.rank(pct=True)
    10000 loops, best of 3: 107 µs per loop
    
    0 讨论(0)
提交回复
热议问题