本文介绍了.groupby& .fillna与中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
# 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 series.fillna(series.median())
# Impute age and assign to titanic['age']
titanic.age = by_sex_class["age"].transform(impute_median)
# Print the output of titanic.tail(10)
print(titanic.tail(10))
我不清楚我们如何将列["age"]从修改(分组)的df by_sex_class分配给原始(未分组)的titanic的df.
I'm unclear how can we assign the column, ["age"] from modified (grouped) df, by_sex_class, to the original (un-grouped) df, titanic.
难道不把作业弄得一团糟吗?
Wouldn't the assignments be jumbled up?
预先感谢您的解释.
推荐答案
新值通过索引与原始数据帧匹配(当您分组时,仍保留原始索引). /p>
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()))
这篇关于.groupby& .fillna与中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!