![](/img/trans.png)
[英]Row-wise comparison of two Pandas DataFrames to extract matched results
[英]Compare columns of two different dataframes row-wise - Pandas
我有两个数据框如下:
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
我想逐行比较 dataframe df1
的列col_1
和 dataframe df2
的列col_2
,看看是否存在由 True 或 False 指示的匹配项。
例如:
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
我尝试了以下但没有给我想要的结果:
df1['Match'] = df1.apply(lambda row: all(i in df2.col_2for i in df1.col_1), axis=1)
有没有办法做到这一点?
检查每一行。
Boolean 将在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.