Search code examples
python-3.xpandascdf

Calculate Percentile from CDF


I have calculated cdf for a data set in pandas df and want to determine the respective percentile from the cdf chart. code for cdf:

def cdf(x):
    df_1=pmf(x)
    df1 = pd.DataFrame()
    df1['pmf'] = df_1['pmf'].sort_index()
    df1['x'] = df_1['x']
    df1['cdf'] = np.cumsum(df1['pmf'])
    return df1

This is the generated cdf df:
enter image description here

Now i want to write a simple logic to fetch the "x" data corresponding to a cdf for determining percentile. Appreciate any help in this regard.


Solution

  • you can do it as below(use df name in place of df below):

    df.loc[df['cdf'] == 0.999083, 'x']
    

    output:

    12.375