pythonpandasstatisticsiqr

How to get the IQR for each value in python


Data is below (df)

id,cost,spend
1,123456,281257
2,150434,838451
3,100435,757565
4,650343,261071
5,-454236,275760
6,-547296,239225

How to get the IQR for each value

output >> id,cost,cost_IQR,spend,spend_IQR

For Z score below is my code

cols = list(df.columns)
cols.remove('id')
#df[cols]

for col in cols:
    col_zscore = col + '_zscore'
    df[col_zscore] = (df[col] - df[col].mean())/df[col].std(ddof=0)

Like the above code i generate the cost_zscore,spend_zscore, How to generate cost_IQR, spend_IQR


Solution

  • df['cost_IQR'] = df.cost.quantile(.75) - df.cost.quantile(.25)
    df['spend_IQR'] = df.spend.quantile(.75) - df.spend.quantile(.25)
    

    or in your for loop:

    df[col + '_IQR'] = df[col].quantile(.75) - df[col].quantile(.25)
    

    Result:

       id    cost   spend   cost_IQR  spend_IQR
    0   1  123456  281257  459257.75  373744.75
    1   2  150434  838451  459257.75  373744.75
    2   3  100435  757565  459257.75  373744.75
    3   4  650343  261071  459257.75  373744.75
    4   5 -454236  275760  459257.75  373744.75
    5   6 -547296  239225  459257.75  373744.75