
Problem –
You have NaN values in a pandas dataframe and you want to replace it with zeros.
Solution –
To fill NaN values with zeros or any other numbers, you can use the fillna() method.
Let’s read a dataset to illustrate it.
import pandas as pd
url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/fruit_prices.csv'
df = pd.read_csv(url)
df.head()

We can see that we have some NaN values in this dataframe. Now let’s fill these NaN values with zeros.
df.fillna(0, inplace=True)

Related Posts –
- Pandas – fillna() method – Handle Missing values in Python
- Pandas – dropna() method -Handle Missing Values in python