如何使用Scala中的Spark DataFrame找到对称的重复列(2列)? [英] How to find the symmetrical duplicate columns(2 columns) using spark dataframe in scala?

查看:91
本文介绍了如何使用Scala中的Spark DataFrame找到对称的重复列(2列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的数据框,其中有两列.

I have the below dataframe which has two columns.

输入数据框:

col1,col2
1,2
2,3
7,0
2,1

在上述数据帧中,第一行和第四行是对称的,应仅考虑一次.我们可以在输出中使用第一行或第四行.

In the above dataframe first row and the fourth row are symmetrical and should be considered only once. We can use either first or the fourth row in the output.

可能的输出数据帧.

可能性1:

col1,col2
2,3
7,0
2,1

可能性2:

col1,col2
1,2
2,3
7,0

推荐答案

您可以在已排序的数组列上调用 dropDuplicates :

You can call dropDuplicates on a sorted array column:

val df2 = df.withColumn(
    "arr", 
    sort_array(array(col("col1"), col("col2")))
).dropDuplicates("arr").drop("arr")

df2.show
+----+----+
|col1|col2|
+----+----+
|   2|   3|
|   1|   2|
|   7|   0|
+----+----+

这篇关于如何使用Scala中的Spark DataFrame找到对称的重复列(2列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆