scala - 如何找到两个数据帧之间的精确匹配和非精确匹配?
问题描述
我有两个数据框。
df1
+--------+-------------------
|id | amount | fee |
|1 | 10.00 | 5.0 |
|2 | 20.0 | 3.0 |
|3 | 90 | 130.0 |
|4 | 120.0 | 35.0 |
df2
+--------+--------------------
|exId | exAmount | exFee|
|1 | 10.00 | 5.0 |
|2 | 20.0 | 3.0 |
|3 | 20.0 | 3.0 |
|4 | 120.0 | 3.0 |
我需要执行以下操作
- 在上面的示例中,查找所有三列都匹配的常见行,例如 id 1,2。
- 查找 (id, exId) 匹配但其他不匹配的常见行,例如上例中的 3 和 4。如果我们确定哪些列不匹配,这将很有用。
所以输出看起来像这样
完全符合
+--------+---------------------------------------------
|id | amount | fee | exId | exAmount | exFee |
|1 | 10.00 | 5.0 | 1 | 10.00 | 5.0 |
|2 | 20.0 | 3.0 | 2 | 20.00 | 3.0 |
+--------+---------------------------------------------
不完全匹配
+--------+------------------------------------------------------------
|id | amount | fee | exId | exAmount | exFee | mismatchFields|
|3 | 90.00 | 130.0 | 3 | 20.00 | 3.0 | [fee, amount]|
|4 | 120.0 | 35.0 | 4 | 120.00 | 3.0 | [fee] |
+--------+------------------------------------------------------------
有什么想法吗?
解决方案
在上面的示例中,查找所有三列都匹配的常见行,例如 id 1,2。
这很容易,您只需在加入时检查所有列是否相等
df1.join(df2, df1("id") === df2("exId") && df1("amount") === df2("exAmount") && df1("fee") === df2("exFee")).show(false)
这应该给你
+---+------+---+----+--------+-----+
|id |amount|fee|exId|exAmount|exFee|
+---+------+---+----+--------+-----+
|1 |10.00 |5.0|1 |10.00 |5.0 |
|2 |20.0 |3.0|2 |20.0 |3.0 |
+---+------+---+----+--------+-----+
查找 (id, exId) 匹配但其他不匹配的常见行,例如上例中的 3 和 4。如果我们确定哪些列不匹配,这将很有用。
为此,您必须检查第一列的相等性,但其余两列的相等性,并在获取最后一列的条件时执行一些操作
import org.apache.spark.sql.functions._
df1.join(df2, df1("id") === df2("exId") && (df1("amount") =!= df2("exAmount") || df1("fee") =!= df2("exFee")))
.withColumn("mismatchFields", when(col("amount") =!= col("exAmount") && col("fee") =!= col("exFee"), array(lit("amount"), lit("fee"))).otherwise(
when(col("amount") === col("exAmount") && col("fee") =!= col("exFee"), array(lit("fee"))).otherwise(array(lit("amount")))
)).show(false)
这应该给你
+---+------+-----+----+--------+-----+--------------+
|id |amount|fee |exId|exAmount|exFee|mismatchFields|
+---+------+-----+----+--------+-----+--------------+
|3 |90 |130.0|3 |20.0 |3.0 |[amount, fee] |
|4 |120.0 |35.0 |4 |120.0 |3.0 |[fee] |
+---+------+-----+----+--------+-----+--------------+
我希望答案有帮助
推荐阅读
- python - 匹配的查询不存在。Django 错误
- sql - SQL 在几个日期之前获取 count(*)
- tableau-api - 在 LOD 中使用 RANK:详细级别表达式不能包含表计算或 Tableau 中的 attr 函数
- php - Yajra Datatables 编辑打印布局
- python - 拟合多项式回归时形状未对齐
- c# - 如何使用 LIST 进行过滤
在 LINQ 中 - networking - What can be done to avoid hosts file entry?
- node.js - Import Express Routes causes Request Body to be Undefined
- windows - no sound difference with multiband compression plugin juce
- perl - 从 perl 脚本运行时,无法获取 wget 命令以将响应结果输出到文件