如何根据主键从表中删除重复行?

问题描述 投票:0回答:1

我在 Dataframe 中加载了一个表,并且尝试将 groupBy 与 PK 一起使用。

df_remitInsert = spark.sql("""SELECT * FROM trac_analytics.mainremitdata""")
df_remitInsert_filter = df_remitInsert.groupBy("LoanID_Serv", "LoanNumber", "Month").count().filter("count > 1").drop('count')

其中,

"LoanID_Serv", "LoanNumber", "Month"
是我的主键。

我想从

df_remitInsert
获取完整数据,这些数据已通过主键进行重复数据删除。

apache-spark pyspark apache-spark-sql
1个回答
0
投票

您可以使用 dropDuplicates 方法。

df_remitInsert_filter = df_remitInsert.dropDuplicates(['LoanID_Serv', 'LoanNumber', 'Month'])
© www.soinside.com 2019 - 2024. All rights reserved.