Method to change the Nan value to 0 in Python

  • 2021-01-14 06:16:04
  • OfStack

This is as follows:


import pandas as pd
df1 = pd.DataFrame([{'col1':'a', 'col2':1}, {'col1':'b', 'col2':2}])
df2 = pd.DataFrame([{'col1':'a', 'col3':11}, {'col1':'c', 'col3':33}])
 
data = pd.merge(left=df1, right=df2, how='left', left_on='col1', right_on='col1')
print (data)
#  will NaN Replace with 0
print (data.fillna(0))

Related articles: