Last active
July 19, 2018 16:07
-
-
Save otaviomguerra/c67dffc76a9f2ac077ab19fe48677a79 to your computer and use it in GitHub Desktop.
Fill in missing data by group with pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#EXAMPLE USING TITANIC DATASET | |
# Create a groupby object: by_sex_class | |
by_sex_class = titanic.groupby(['sex', 'pclass']) | |
# 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment