Search code examples
pythonpandasdataframefillna

Confusion with fillna() when None and NaN values in dataframe


When I apply fillna('New_Value) to the df below it fills all the None and NaN values except Column:D at Index 1.

So what is the reason?

Here is my code

df=pd.DataFrame([{'A':None,'B':False,'C':1,'D':'a','E':np.NaN,'F':True,'G':'a','H':np.NaN},{'A':1,'B':2,'C':3,'D':'None','E':1,'F':True,'G':'b'},
                 {'A':False,'B':None,'C':None,'D':True,'E':2,'F':True,'G':'b','H':None},
                    {'A':'a','B':'b','C':1,'D':'b','E':3,'F':False,'G':'c','H':np.NaN},
                     {'A':None,'B':4,'C':6,'D':'c','E':None,'F':False,'G':'c','H':None},
                     {'A':None,'B':4,'C':6,'D':'c','E':None,'F':True,'G':'d','H':True},
                     {'A':True,'B':False,'C':True,'D':False,'E':True,'F':False,'G':False}])

Solution

  • You wrote None in that case as string. Remove the quotes '

    df = pd.DataFrame([{'A':None,'B':False,'C':1,'D':'a','E':np.NaN,'F':True,'G':'a','H':np.NaN},{'A':1,'B':2,'C':3,'D':None,'E':1,'F':True,'G':'b'}, ...)
    

    If you cannot modify the df, replace the string:

    df = df.replace({'None':'New_Value'})