I have two dataframes as follows:
df1 =
col_1 val_1
0 4.0 0.89
1 4.0 0.56
2 49.0 0.7
3 49.0 1.23
4 52.0 0.8
df2 =
col_2 val_2
0 3.0 9
1 4.0 23.0
2 49.0 85
3 50.0 34
4 52.0 75
I want to compare the columns col_1
of the dataframe df1
and col_2
of the dataframe df2
row-wise and see if there is a match which is indicated by True or False.
for example:
df1 =
col_1 val_1 Match
0 4.0 0.89 False
1 4.0 0.56 True
2 49.0 0.7 True
3 49.0 1.23 False
4 52.0 0.8 True
I tried the following but it did not give me the desired results:
df1['Match'] = df1.apply(lambda row: all(i in df2.col_2for i in df1.col_1), axis=1)
Is there a way to do this?
CodePudding user response:
Check each row.
Boolean will be assigned in the new column match
df1['Match'] = df1['col_1'] == df2['col_2']
Output:
col_1 val_1 Match
0 4.0 0.89 False
1 4.0 0.56 True
2 49.0 0.70 True
3 49.0 1.23 False
4 52.0 0.80 True
CodePudding user response:
df1 = pd.DataFrame({'col_1':[4.0, 4.0, 49.0, 49.0, 52.0],
'val_1':[0.89, 0.56, 0.7, 1.23, 0.8]})
df2 = pd.DataFrame({'col_2':[3.0, 4.0, 49.0, 50.0, 52.0],
'val_2':[9, 23.0, 85, 34, 75]})
df1['Match'] = df1['col_1'] == df2['col_2']