# 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):
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()))
I will recommend using this
df['age'].fillna(df.groupby(["sex","pclass"])['age'].transform('median'),inplace=True)