.groupby & .fillna With Median
# Create a groupby object: by_sex_class by_sex_class = titanic.groupby(['sex','pclass']).count() # Write a function that imputes median def impute_median(series): return serie
Solution 1:
I will recommend using this
df['age'].fillna(df.groupby(["sex","pclass"])['age'].transform('median'),inplace=True)
Solution 2:
The new values are matched to the original dataframe by the index (when you group, you still keep the original index).
df['age'] = df.groupby(["sex","pclass"])['age'].transform(lambda x: x.fillna(x.median()))
Post a Comment for ".groupby & .fillna With Median"