nnnnmmm nnnnmmm - 7 months ago 14
Python Question

find uncommon rows between two csv files having common field

I have one csv file with following data

code
AAA
BBA
CCC


and another csv file with different structure but common column:

code
AAA
BBA
CCF


I want to find what code in first does not exist in the second dataframe. In this case CCC.

How can i do that?

Answer

Since you are using the pandas tag, I will assume you are after a solution involving pandas. If you had read both CSV files in two different dataframes, then assuming that the first CSV is read in df_1 and the second one in df_2, you could do this:

>> df_1[~df_1['code'].isin(df_2['code'])][['code']]

or

>> set(df_1['code'].values.tolist()) - set(df_2['code'].values.tolist())

or

>> set(list(df_1['code'])) - set(list(df_2['code']))

I hope this helps!