+-
数据如下(df)
id,cost,spend
1,123456,281257
2,150434,838451
3,100435,757565
4,650343,261071
5,-454236,275760
6,-547296,239225
如何获得每个值的IQR
输出>>。id,cost,cost_IQR,spend,spend_IQR
以下是我的代码
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
1
投票
投票
df['cost_IQR'] = df.cost.quantile(.75) - df.cost.quantile(.25)
df['spend_IQR'] = df.spend.quantile(.75) - df.spend.quantile(.25)
或在你的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