Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

replace all column values not in a list python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

             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.

>Solution :

You can use where:

df_new = df.where(df.isin(my_list))

output:

  Animal  Color      Note
0    NaN    NaN  Friendly
1    Dog  White       NaN
2    NaN    NaN       NaN

That said, df[~df.isin(my_list)] = 'NA' should also work for in place modification, but replace with a ‘NA’ string, not a real NaN.

Be careful, df_new = df[~df_in.isin(my_list)] = "NA" doesn’t make sense as you have a double assignment (and it is unclear what is df and what is df_in)!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading