python3 single column dataframe compare

I have 2 pandas data frames, each one with single column (‘id’) as per below, id column may have duplicates also. df1:

id

1
2
3
4
5

df2:

id

1
2
3

Output: id

4
5

I’m reading 2 files, i’m not able to achieve desired output using merge. Any help is highly appreciated.

import pandas as pd
colnames = ['id']
df1 = pd.read_csv(r'file1.csv', names=colnames)
df2 = pd.read_csv(r'file2.csv', names=colnames)
###df = df1.merge(df2, how='right')

Answer

You could use set:

>>> set(df1['id']) - set(df2['id'])
{4, 5}
>>> 

Or:

>>> df1.loc[~df1['id'].isin(df2['id'])]
   id
3   4
4   5
>>>