比较两个数据范围并显示不同的数据

发布于 2025-01-21 09:45:41 字数 1727 浏览 4 评论 0原文

我有两个数据范围,我想比较两个列的值并显示不同的值,例如:比较此表1

ABCD
O12E12
E1 2 O13E11
O1 1 O12E1 2 E10
O15 E1 5 E2 2O12
O1 2 O12E23
O12E22
O15E21
O28E12
O28E10
O20E11
O22E19
E1 2 O24 O2 9E12
O2 E22E21
O29E2 9 E24
4 O2 4 O2O2 2E2E2

带有此表2

ABC CD
O12E12
O12E23
O22E14
O29E24

我尝试过

cond= [table1.A ==  table2.A, table1.C ==  table2.C, table1.D ==  table2.D]
join = table1.join(table2,cond,"leftsemi")

,并且由于其中有很多数据,我不知道如何检查我所拥有的结果是否正确

i have two dataframes and i want to compare the values of two columns and display those who are different, for exemple: compare this Table 1

ABCD
O12E12
O13E11
O12E10
O15E22
O12E23
O12E22
O15E21
O28E12
O28E10
O20E11
O22E14
O29E12
O22E21
O29E24
O22E22

with this table 2

ABCD
O12E12
O12E23
O22E14
O29E24

i tried

cond= [table1.A ==  table2.A, table1.C ==  table2.C, table1.D ==  table2.D]
join = table1.join(table2,cond,"leftsemi")

and since i have a lot of data in it, i don't know how to check if the result i've got is correct

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2025-01-28 09:45:41

由于您的DataFrames具有相同的架构,因此您可以使用减去

df1
df1 = spark.createDataFrame([
    (1, 2, 3, 4),
    (5, 6, 7, 8),
], ['a', 'b', 'c', 'd'])

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  1|  2|  3|  4|
|  5|  6|  7|  8|
+---+---+---+---+
df2
df2 = spark.createDataFrame([
    (5, 6, 7, 8),
], ['a', 'b', 'c', 'd'])

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  5|  6|  7|  8|
+---+---+---+---+
减去获取<<<<<代码> df1 ,但不存在df2
df1.subtract(df2).show()

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  1|  2|  3|  4|
+---+---+---+---+

Since your dataframes has the same schema, you can use subtract

df1
df1 = spark.createDataFrame([
    (1, 2, 3, 4),
    (5, 6, 7, 8),
], ['a', 'b', 'c', 'd'])

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  1|  2|  3|  4|
|  5|  6|  7|  8|
+---+---+---+---+
df2
df2 = spark.createDataFrame([
    (5, 6, 7, 8),
], ['a', 'b', 'c', 'd'])

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  5|  6|  7|  8|
+---+---+---+---+
subtract to get data that exists in df1 but does not exists in df2
df1.subtract(df2).show()

+---+---+---+---+
|  a|  b|  c|  d|
+---+---+---+---+
|  1|  2|  3|  4|
+---+---+---+---+
弃爱 2025-01-28 09:45:41

从一个dataframe

check_list = list = list(df2.apply(tuple,axis = 1))

使用它与其他

df1 = df1 = df1 [〜df1.apply(tuple(tuple)(tuple) ,轴= 1).isin(check_list)]

Create a list of tuple from one dataframe

check_list = list(df2.apply(tuple, axis=1))

and use it to compare with other

df1 =df1[~df1.apply(tuple, axis=1).isin(check_list)]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文