Skip to content

Instantly share code, notes, and snippets.

@ant358
Created February 24, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ant358/eca49870e5362a3f165b86d0defaa0bc to your computer and use it in GitHub Desktop.
Save ant358/eca49870e5362a3f165b86d0defaa0bc to your computer and use it in GitHub Desktop.
def replace_all_NaN(df):
""" If you are confident that numbers can be replaced with 0 and
objects can be replaced by No_columnname this function will do that over the whole data frame
will add more data types as I come across them. It prints info() when finished to check it
has captured them all"""
for col in df:
if df[col].dtype == 'object' and df[col].isna().sum() > 0:
df[col] = df[col].fillna('No_' + col)
elif df[col].dtype == 'float64' and df[col].isna().sum() > 0:
df[col] = df[col].fillna(0.0)
elif df[col].dtype == 'int64' and df[col].isna().sum() > 0:
df[col] = df[col].fillna(0)
elif df[col].isna().sum() > 0:
print('Uncoded dtype({}), please update function'.format(df[col].dtype))
print(df.info(verbose=True))
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment