gpt4 book ai didi

python - 从 PySpark 中的数据框中删除重复项

转载 作者:太空狗 更新时间:2023-10-29 17:10:55 25 4
gpt4 key购买 nike

我在本地处理 pyspark 1.4 中的数据帧,并且在使 dropDuplicates 方法起作用时遇到问题。它不断返回错误:

"AttributeError: 'list' object has no attribute 'dropDuplicates'"

不太清楚为什么我似乎遵循了 latest documentation 中的语法.

#loading the CSV file into an RDD in order to start working with the data
rdd1 = sc.textFile("C:\myfilename.csv").map(lambda line: (line.split(",")[0], line.split(",")[1], line.split(",")[2], line.split(",")[3])).collect()

#loading the RDD object into a dataframe and assigning column names
df1 = sqlContext.createDataFrame(rdd1, ['column1', 'column2', 'column3', 'column4']).collect()

#dropping duplicates from the dataframe
df1.dropDuplicates().show()

最佳答案

这不是导入问题。您只需在错误的对象上调用 .dropDuplicates() 即可。虽然 sqlContext.createDataFrame(rdd1, ...) 的类是 pyspark.sql.dataframe.DataFrame,但在您应用 .collect() 之后它是一个普通的 Python list,列表不提供 dropDuplicates 方法。你想要的是这样的:

 (df1 = sqlContext
.createDataFrame(rdd1, ['column1', 'column2', 'column3', 'column4'])
.dropDuplicates())

df1.collect()

关于python - 从 PySpark 中的数据框中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com