如何获取python中每个值的IQR

如何获取python中每个值的IQR,python,pandas,statistics,iqr,Python,Pandas,Statistics,Iqr,数据低于df id,cost,spend 1,123456,281257 2,150434,838451 3,100435,757565 4,650343,261071 5,-454236,275760 6,-547296,239225 如何获得每个值的IQR 输出>>id,成本,成本,支出,支出 下面是我的代码 cols = list(df.columns) cols.remove('id') #df[cols] for col in cols: col_zscore = col

数据低于df

id,cost,spend
1,123456,281257
2,150434,838451
3,100435,757565
4,650343,261071
5,-454236,275760
6,-547296,239225
如何获得每个值的IQR

输出>>id,成本,成本,支出,支出

下面是我的代码

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)
像上面的代码一样,我生成cost_zscore,spend_zscore,如何生成cost_IQR,spend_IQR

或者在for循环中:

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

   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
   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