首页 > 解决方案 > 从 pyspark 数据框中删除具有相同值但在不同列中的重复行

问题描述

我想从两列中删除重复的行。包含两个值的行具有相同的记录,但顺序相反。

|--------------|-------------------|
|   name       |   alt_name        |
|----------------------------------|
|  a10.samsung | a20.samsung       |
|  x.iphone    |  xr.iphone        |
|  3.nokia     |  5.nokia          |
| a20.samsung  | a10.samsung       |
| 5.nokia      | 3.nokia           |
|  xr.iphone   |  x.iphone         |
------------------------------------

我想要以下输出;

|--------------|-------------------|
|   name       |   alt_name        |
|----------------------------------|
|  3.nokia     |  5.nokia          |
|  a10.samsung | a20.samsung       |
|  x.iphone    |  xr.iphone        |
------------------------------------

标签: pysparkpyspark-dataframes

解决方案


你可以使用 spark sql 来做到这一点:

我假设您的原始数据框名称为 mobiles 和删除重复项的代码:

mobiles.createTempView('tablename')

newDF= spark.sql("select * from tablename where name<=alt_name")

newDF.show()

推荐阅读