我想用每个病人的平均值来分配所有的SkinThickness
零值
在Age
的一定范围内
所以我把数据框按Age
分组,得到每个年龄段SkinThickness
的平均值
为了将SkinThickness
列中的每个零值赋给根据年龄分组计算的相应平均值
ageSkinMean = df_clean.groupby("Age_Class")["SkinThickness"].mean()
>>> ageSkinMean
Age_Class
21-22 years 82.163399
23-25 years 103.171429
26-30 years 91.170254
31-38 years 80.133028
39-47 years 73.685851
48-58 years 89.130233
60+ years 40.899160
Name: Insulin, dtype: float64
现在我运行的代码太少了。。。使用iterrows()
花费的时间太长
start = time.time()
for i, val in df_clean[df_clean.SkinThickness == 0].iterrows():
if val[7] < 22:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[0]
elif val[7] < 25:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[1]
elif val[7] < 30:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[2]
elif val[7] < 38:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[3]
elif val[7] < 47:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[4]
elif val[7] < 58:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[5]
else:
df_clean.loc[i, "SkinThickness"] = ageSkinMean[6]
print(time.time() - start)
我想知道是否有熊猫优化这样的代码块运行更快
您可以使用pandas transform函数将SkinThickness 0值替换为平均值
在df\u clean中SkinThickness==0的所有行现在的SkinThickness将等于其年龄组的平均值
相关问题 更多 >
编程相关推荐