Search code examples
pythonpandasnumpydatatable

How can i divide column into different quantiles in python


I have data like this

High Low Price
10 5 7

I want to know at which quantile is the price What are the quantile boundaries

I tried posting code but getting following error again and again

Your post appears to contain code that is not properly formatted as code enter image description here Expected Output:

High Low Price Qtr Qtr_Ranges
10 5 8 Q3 10, 8.75, 7.5,6.25,5
10 5 9 Q1 10, 8.75, 7.5,6.25,5
10 5 6 Q4 10, 8.75, 7.5,6.25,5
10 5 6.5 Q3 10, 8.75, 7.5,6.25,5

Can you guys help? Thanks!!!


Solution

  • Try this one.

    a = df[['High','Low','Price']].values
    bins = np.quantile(a[:,:2], q=[0,0.25,.5,.75,1.], axis=1)
    qrng = bins.T[:,::-1].tolist()
    df['Qtr_Ranges'] = qrng
    c =np.apply_along_axis(np.searchsorted,1, bins.T, a[:,2], side='left')
    #print(c)
    qtr = np.diag(c)
    df['Qtr'] = qtr
    df['Qtr'] = 'Q' + df['Qtr'].astype(str)