How to make pandas interpret NA as null but only for some of the selected columns?

106 Views Asked by At

I have a dataset with more than 40 columns and for some of the columns 'NA' is a valid entry which means 'Not Applicable'. But for rest of the columns 'NA' can be considered as NULL. Currently pandas considers all 'NA' entries as NaN which is not correct.

I am aware of < keep_default_na=False > but this can make all NA as non-null, which is again not correct. for example: if I have 10 columns and for 4 of the columns I want to retain original entry 'NA' as it is, without being considered as NaN. For rest of 6 columns 'NA' entry should be considered as NaN.

Is there any other way by which I can prevent pandas from considering 'NA' as null for some specific columns ?

1

There are 1 best solutions below

0
jezrael On BEST ANSWER

Is there any other way by which I can prevent pandas from considering 'NA' as null for some specific columns ?

No, you can only replace NaN to some another value in selected column:

cols = ['col1','col2']

df = df.fillna(dict.fromkeys(cols, 'NA'))