Skip to content Skip to sidebar Skip to footer

Find Pandas Quartiles Based On Another Column

I have a dataframe: Av_Temp Tot_Precip 278.001 0 274 0.0751864 270.294 0.631634 271.526 0.229285 272.246 0.0652201 273 0.0840059 270.463 0.0602944 269.983 0.103563 268.774

Solution 1:

I'm only splitting you data into halves rather than deciles here due to the small example dataset, but everything should work the same if you just increase the number of bins in the initial cut:

# Change this to 10 to get deciles
df['Temp_Halves'] = pd.qcut(df['Av_Temp'], 2)

def get_quartiles(group):
    # Add retbins=True to get the bin edges
    qs, bins = pd.qcut(group['Tot_Precip'], [.25, .5, .75], retbins=True)
    # Returning a series from a function means groupby.apply() will 
    #   expand it into separate columns
    return pd.Series(bins, index=['Precip_25', 'Precip_50', 'Precip_75']

df.groupby('Temp_Halves').apply(get_quartiles)
Out[21]: 
                    Precip_25  Precip_50  Precip_75
Temp_Halves                                        
[268.774, 270.995]   0.048010   0.064875   0.095036
(270.995, 278.001]   0.038484   0.070203   0.081801

Post a Comment for "Find Pandas Quartiles Based On Another Column"