prashant
prashant

Reputation: 271

Compare a column in 2 different dataframes in pandas (only 1 column is same in both dataframes)

I have 2 dataframes df1 and df2 and I want to compare 'col1' of both dataframes and get the rows from df1 where 'col1' values don't match. Only 'col1' is common in both dataframes.

Suppose I have:

df1 = pd.DataFrame({
    'col1': range(1, 6),
    'col2': range(10, 60, 10),
    'col3': [*'abcde']
})
df2 = pd.DataFrame({
    'col1': range(1, 4),
    'cola': ['Aa', 'bcd', 'h'],
    'colb': [12, 'sadf', 'dd']
})

print(df1)

   col1  col2 col3
0     1    10    a
1     2    20    b
2     3    30    c
3     4    40    d
4     5    50    e

print(df2)

   col1 cola  colb
0     1   Aa    12
1     2  bcd  sadf
2     3    h    dd

I want to get:

   col1  col2 col3
0     4    40    d
1     5    50    e

Upvotes: 0

Views: 61

Answers (2)

Anurag Dabas
Anurag Dabas

Reputation: 24322

Just use this:-

df1[~df1['col1'].isin(df2['col1'])]

Now if you print above code you will get your desired output:-

   col1  col2 col3
3     4    40    d
4     5    50    e

Upvotes: 1

piRSquared
piRSquared

Reputation: 294546

Quick and Dirty

df1.append(df1.merge(df2.col1)).drop_duplicates(keep=False)

   col1  col2 col3
3     4    40    d
4     5    50    e

Upvotes: 1

Related Questions