I want to assign All SkinThickness
zero value with the mean of each patient lies in certain range of Age
.
So I grouped data-frame by Age
to get the mean of SkinThickness
for each age range.
In order to assign every Zero value in SkinThickness
Column to the corresponding mean value computed from the age grouping.
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
Currently I'm running such insufficient code ... which takes too long time for using 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)
I wonder if there exist any pandas optimization to such block of code to run faster
You can use pandas transform function to replace SkinThickness 0 value with mean values
age_skin_thickness_mean = df_clean.groupby('Age_Class')['SkinThickness'].mean()
def replace_with_mean_thickness(row):
row['SkinThickness'] = age_skin_thickness_mean[row['Age_Class']]
return row
df_clean.loc[df_clean['SkinThickness'] == 0] = df_clean.loc[df_clean['SkinThickness'] == 0].transform(replace_with_mean_thickness, axis=1)
All rows having SkinThickness == 0 in df_clean will now have SkinThickness equal to their age group mean value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.