为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 pyspark 数据框中使用 write.partitionBy 时删除重复项?

如何在 pyspark 数据框中使用 write.partitionBy 时删除重复项?

慕桂英3389331 2023-10-18 20:39:56
我有一个数据框,如下所示:|------------|-----------|---------------|---------------||    Name    |   Type    |  Attribute 1  |  Attribute 2  ||------------|-----------|---------------|---------------||   Roger    |     A     |     X         |       Y       ||------------|-----------|---------------|---------------||   Roger    |     A     |     X         |       Y       ||------------|-----------|---------------|---------------||   Roger    |     A     |     X         |       Y       ||------------|-----------|---------------|---------------||   Rafael   |     A     |     G         |       H       ||------------|-----------|---------------|---------------||   Rafael   |     A     |     G         |       H       ||------------|-----------|---------------|---------------||   Rafael   |     B     |     G         |       H       ||------------|-----------|---------------|---------------|我想根据名称和类型对此数据框进行分区并将其保存到磁盘目前的代码行看起来像这样,df.write.partitionBy("Name", "Type").mode("append").csv("output/", header=True)输出被正确保存,但有重复的行,如下所述在文件夹中/输出/罗杰/A|---------------|---------------||  Attribute 1  |  Attribute 2  ||---------------|---------------||     X         |       Y       ||---------------|---------------||     X         |       Y       ||---------------|---------------||     X         |       Y       ||---------------|---------------|/输出/拉斐尔/A|---------------|---------------||  Attribute 1  |  Attribute 2  ||---------------|---------------||     G         |       H       ||---------------|---------------||     G         |       H       ||---------------|---------------|/输出/拉斐尔/B|---------------|---------------||  Attribute 1  |  Attribute 2  ||---------------|---------------||     G         |       H       ||---------------|---------------| 如您所见,此 csv 包含重复项。使用 write.partitionbY 时如何删除这些重复项?
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

.distinct()写作前使用。

df.distinct().write.partitionBy("Name", "Type").mode("append").csv("output/", header=True)



查看完整回答
反对 回复 2023-10-18
  • 1 回答
  • 0 关注
  • 55 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信