Pandas filling NaN values with a string value and keeping None values as the same

204 Views Asked by At

I have a dataframe with mixed datatypes. I would like to fill NaN values but keep None values as the same.

I tried to use fillna() method from pandas but the function is filling both NaN and None values. I want to fill NaN with a specific string value like 'x', instead of numbers. And keep None values as the same.

For example:
   A     B     C
0  12    0    None
1  None  NaN  None
2  NaN   9.8  1
3  0     NaN  1

Expected:
   A     B     C
0  12    0    None
1  None  x    None
2  x     9.8  1
3  0     x    1
2

There are 2 best solutions below

1
venkata krishnan On
df = pd.DataFrame([[None, 3], ["", np.nan]]) #created a dummy dataframe with these values

and then use this below code segment to change None to 'None' (a string) and then replace NA with which ever values you want to replace it with.

the finally converting it back to None (not a string)

df.replace({None:'None'}).fillna("x").replace({'None':None})

result looks like this

0 None  3.0 
1       x
2
PV8 On

If dfis your dataframe:

df = df.replace({None:'None'}).fillna('x')

In the first step you will replace the None with a string 'None', then in the second step you will fill NaN with the string 'x'.

If the NaN are also identified as None try:

import numpy as np
df = df.replace({np.nan: 'x'})

followed by Replacing Pandas or Numpy Nan with a None to use with MysqlDB