Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding rows with same column values in pandas dataframe

I have two dataframes with different column size, where four columns can have the same values in both dataframes. I want to make a new column in df1, that takes the value 1 if there is a row in df2 that has the same values for column 'A','B','C', and 'D' as a row in df1. If there isn't such a row, I want the value to be 0. Rows 'E' and 'F' are not important for checking the values.

Is there a pandas function that can do this, or do I have to this in a loop.

For example:

df1 =
A    B    C    D    E    F
1    1    20   20   3    2
1    1    12   14   1    3
2    1    13   43   4    3
2    2    12   34   1    4

df2 =
A    B    C    D    E    
1    3    12   14   2    
1    1    20   20   4   
2    2    21   31   5    
2    2    12   34   8    

expected output:

df1 =
A    B    C    D    E    F    Target
1    1    20   20   3    2    1
1    1    12   14   1    3    0
2    1    13   43   4    3    0
2    2    12   34   1    4    1
like image 468
Bram Zijlstra Avatar asked Aug 06 '26 08:08

Bram Zijlstra


1 Answers

This is fairly simple. If you check whether two DataFrames are equal, it checks if each element is equal to the respective element.

col_list = ['A', 'B', 'C', 'D']
idx = (df1.loc[:,  col_list] == df2.loc[:,  col_list]).all(axis=1)

df1['new_row'] = idx.astype(int)
like image 173
Little Bobby Tables Avatar answered Aug 09 '26 02:08

Little Bobby Tables



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!