Search code examples
pythonpandasquantile

what's the inverse of the quantile function on a pandas Series?


The quantile functions gives us the quantile of a given pandas series s,

E.g.

s.quantile(0.9) is 4.2

Is there the inverse function (i.e. cumulative distribution) which finds the value x such that

s.quantile(x)=4

Thanks


Solution

  • Use scipy.stats.percentileofscore:

    # libs required
    from scipy import stats
    import pandas as pd
    import numpy as np
    
    # generate ramdom data with same seed (to be reproducible)
    np.random.seed(seed=1)
    df = pd.DataFrame(np.random.uniform(0, 1, (10)), columns=['a'])
    
    # quantile function
    x = df.quantile(0.5)[0]
    
    # inverse of quantile
    stats.percentileofscore(df['a'], x)