0

I have a .csv file which has both nan and blanks in it, I need to replace both of them separately, say 'nan' with 'None' and blanks with 'NA', how do I achieve it.

Maybe I wasn't clear earlier, I need to replace both the nan and the blank with 2 different values. nan is a separate string value in my file. Using .replace is not helping, because it's treating all blanks as nan and replacing them with 'None' and vice versa, so in the end I've all values either None or NA, but I NEED DIFFERENT ENTRIES FOR NAN AND BLANKS. This isn't a duplicate question Performing what's suggested on Replacing few values in a pandas dataframe column with another value, didn't help:

Col1  Col2
neb   abc
ijd   nan
      987
2938
nan   909

Expected Result :

Col1  Col2
neb   abc
ijd   None
NA    987
2938  NA
None  909

I'm using :

df['Col1'] = df['Col1'].replace('nan','None')
df['Col1'] = df['Col1'].replace(np.nan,'NA')
df['Col2'] = df['Col2'].replace('nan','None')
df['Col2'] = df['Col2'].replace(np.nan,'NA')
mamta_rao
  • 29
  • 4

1 Answers1

1

If you don't want pandas to replace string "nan" and blanks with nan when reading csv you can set na_filter=False while reading csv and then replace blanks and string "nan" with specified values:

df=pd.read_csv(fl,na_filter=False)
df=df.replace({'nan':'None', '':'NA'})

print(df)

   Col1  Col2
0   neb   abc
1   ijd  None
2    NA    NA
3  2938    NA
4  None   909
YevKad
  • 650
  • 6
  • 13