Search code examples
pythondataframefillna

Set all None and NaN values to 0 in a datframe - 'NoneType' object has no attribute 'isnull'


# Check for None and NaN values in the dataframe and clean if needed
print("Any NaN values in the dataframe? True or False",  end='\n')
display(dfManual_With_NaN.isnull().values.any())    
print("",  end='\n')

Output: True

print("Total Number of NaN values in the dataframe",  end='\n')
display(dfManual_With_NaN.isnull().sum().sum())
print("",  end='\n')

Output: 1

print("Display the total number of rows with a NaN",  end='\n')
df_NaN_Rows = dfManual_With_NaN[dfManual_With_NaN.isnull().T.any().T]  
display(df_NaN_Rows.head(100))
print("",  end='\n')


Output:

enter image description here

# Update None and NaN to zero
dfManual_Booked = dfManual_With_NaN.fillna(value='NaN', inplace=True) # Replace None values with NaN
dfManual_Booked = dfManual_Booked.fillna(0)              # Replace NaN with 0. 

Errors here: 'NoneType' object has no attribute 'isnull'

So I update None values to NaN then look to set all NaN to 0. Any guidance as to why this is failing?


Solution

  • When using inplace=True, you are performing the operation on the same dataframe instead of returning a new one (also the function call would return None when inplace=True).

    Also NaN and None are treated the same for the fillna call, so just do dfManual_Booked = dfManual_Booked.fillna(0) would suffice. (Or just dfManual_Booked.fillna(0, inplace=True))