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

Compare two DataFrame columns of lists of strings (A & B) to find if any in B are NOT in A for adding to Col C

d = {'Col A': [['Singapore','Germany','UK'],['Ireland','Japan','Australia'],['India','Korea','Vietnam']], 'Col B': [['Singapore','Germany','UK'],['Ireland','Japan'],['India','Mexico','Argentina']]}

df = pd.DataFrame(data=d)

I’m trying to compare these two columns and return a new column, Col C, that contains any strings that are present in Col B but NOT present in Col A. So row 1 being the same returns no value, row 2 where A contains UK returns no value, but row 3 returns ‘Mexico’ and ‘Argentina’ but not ‘Korea’ or Vietnam.

I’ve tried creating a separate column out of Col A that eliminates countries from Col A that are not present in Col B, like Australia because it’s okay for countries to be in Col A that are NOT in Col B. And then a list comprehension to identify unique strings between the two that can then be added to Col C. But I feel like there must be a simpler method.

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

>Solution :

You can use np.setdiff1d.

for index, row in df.iterrows():
    df.at[index, 'Col C'] = np.setdiff1d(row['Col B'], row['Col A'])
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