How to Replace NaN values with Zeros in Pandas

Spread the love

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 –

  1. Pandas – fillna() method – Handle Missing values in Python
  2. Pandas – dropna() method -Handle Missing Values in python

Rating: 1 out of 5.

Leave a Reply