Search code examples
pythonpandasnanfillna

fillna() is not replacing NaN values even after using inplace=True


I am trying to replace NaN value in my 'price' column of my dataset, I tried using:

avg_price = car.groupby('make')['price'].agg(np.mean) # calculating average value of the price of each car company model

new_price= car['price'].fillna(avg_price,inplace=True)

car['price']=new_price

The code runs well without any error, but on checking, I can still see the NaN values in the dataset. Dataset snap shot is attached below:

This is my original dataset as well as after running fillna() its the same


Solution

  • Are you trying to fill the NaN with a grouped (by make) average? Will this work?

    df.loc[df.price.isnull(), 'price'] = df.groupby('make').price.transform('mean')