Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
27 lines (20 sloc) 869 Bytes
import pandas
import numpy
def imputation(filename):
# Pandas dataframes have a method called 'fillna(value)', such that you can
# pass in a single value to replace any NAs in a dataframe or series. You
# can call it like this:
# dataframe['column'] = dataframe['column'].fillna(value)
# Using the numpy.mean function, which calculates the mean of a numpy
# array, impute any missing values in our Lahman baseball
# data sets 'weight' column by setting them equal to the average weight.
# You can access the 'weight' colum in the baseball data frame by
# calling baseball['weight']
baseball = pandas.read_csv(filename)
baseball['weight'] = baseball['weight'].fillna(numpy.mean(baseball['weight']))
return baseball
if __name__ == '__main__':
imputed = imputation("Master.csv")