pythonpandasisnullfillna

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


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 ?


Solution

  • 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'))