0

I have a df as shown below.

df1:

ID     Age_days    N_30     N_31_90     N_91_180      
1      201         60       15          30            
2      20          0        15          5             
3      800         0        0           10            
4      100         0        0           0             
5      600         0        6           5             
6      800         0        0           15            
7      500         10       10          30 

df2:

 ID     Age_days    N_30     N_31_90     N_91_180      
    1      201         60       15          30            
    2      20          0        15          5
    4      100         0        0           0
    6      800         0        0           15

where ID is the primary key for both df1 and df2.

From which I would like to select the rows which are in df1 and not in df2.

Expected Output:

ID     Age_days    N_30     N_31_90     N_91_180             
3      800         0        0           10                        
5      600         0        6           5                        
7      500         10       10          30 

I tried below code:

df3 = df1[~df1.isin(df2)].dropna()

I referred below slack questions. pandas get rows which are NOT in other dataframe

common = df1.merge(df2,on=['col1','col2'])
print(common)
df1[(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))]

My doubt is what should be col1 and col2 in my case.

Danish
  • 2,719
  • 17
  • 32

0 Answers0