Created
February 24, 2019 10:05
-
-
Save ant358/eca49870e5362a3f165b86d0defaa0bc to your computer and use it in GitHub Desktop.
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
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