Skip to content

Instantly share code, notes, and snippets.

@otaviomguerra
Last active July 19, 2018 16:07
Show Gist options
  • Save otaviomguerra/c67dffc76a9f2ac077ab19fe48677a79 to your computer and use it in GitHub Desktop.
Save otaviomguerra/c67dffc76a9f2ac077ab19fe48677a79 to your computer and use it in GitHub Desktop.
Fill in missing data by group with pandas
#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