I have a df that looks like below:
Animal Color Note
0 Cat Brown Friendly
1 Dog White Furry
2 Rabbit Brown Furry
Now I want the code to check values in all columns and comparing against a list, replace the values that are not in the list with "NA". So the list is
my_list = ['Dog', 'White', 'Friendly']
And the desired output is:
Animal Color Note
0 NA NA Friendly
1 Dog White NA
2 NA NA Furry
I found a similar question in below link How to replace all values in a Pandas Dataframe not in a list?
So as suggested there, I tried the below
df_new = df[~df.isin(my_list)] = "NA"
But it gives me "NA" as a result, not the desired df. Could someone please help me with how to fix this? Much appreciated.