How to Replace NaN with Average of Columns in Pandas?

Spread the love

Problem –

You want to replace NaN with average of columns in Pandas.

Solution –

Read a dataset to work with.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/fruit_prices.csv'
df = pd.read_csv(url)
df.head()

To replace the NaN with average of the columns you have to write.

df.fillna(df.mean(), inplace=True)

Rating: 1 out of 5.

Leave a Reply