我正在尝试将数据集中的所有值转换为分类值,我希望将所有数值分类为低、平均值或高,具体取决于它们的分位数值。
因此,如果该值低于系列的 25%,则会转换为“低”
我尝试使用分配然后应用我提供的函数:
def turn_into_categorical(row):
quantile_level = [.25, .5, .75]
for r in row:
cut = refugees_T_F_V_P_full_data.r.quantile(quantile_level)
if r >= cut[.75]:
return "High"
elif r >= cut[.25] and r < cut[0.75]:
return "Average"
else:
return "Low"
refugees_T_F_V_P_full_data.apply(turn_into_categorical, axis = 1)
Run Code Online (Sandbox Code Playgroud)
但是,该代码不能很好地工作。我也通过 iterrows 尝试过,但我想知道是否有更快的方法?
这是我想要转换的数据,除年份和月份之外的所有数字都应根据其分位数值分为低、中、高。
Year Month Central Equatoria Eastern Equatoria Gogrial Jonglei
0 2014 10 6.0 1.0 0.0 3.0
1 2014 11 4.0 3.0 0.0 12.0
2 2014 12 3.0 5.0 0.0 11.0
3 2015 1 7.0 2.0 0.0 …Run Code Online (Sandbox Code Playgroud)