Search code examples
pythonpandasdataframedata-analysisfillna

changes not saved after using fillna


I have several null values in a column:

print(df['col'].isnull().sum(axis = 0))

I am trying to replace them like this:

df['col'].fillna(value='no_val')
#df['col'].value_counts()
print(df['col'].isnull().sum(axis = 0))

but it doesn't work and still shows the same number of null values after using .fillna as well. If I change it like this:

df = df['col'].fillna(value='no_val')

it would start giving me a key error for 'col' in the next line. What am I doing wrong?


Solution

  • You can use inplace=True inside fillna method to reflect output

    df['col'].fillna(value='no_val',inplace=True)
    

    Or you can reassing column

    df['col']= df['col'].fillna(value='no_val')