I’m messing around with dataframes in pyspark 1.4 locally and am having issues getting the dropDuplicates
method to work. It keeps returning the error:
“AttributeError: ‘list’ object has no attribute ‘dropDuplicates'”
Not quite sure why as I seem to be following the syntax in the latest documentation.
JavaScript
x
9
1
#loading the CSV file into an RDD in order to start working with the data
2
rdd1 = sc.textFile("C:myfilename.csv").map(lambda line: (line.split(",")[0], line.split(",")[1], line.split(",")[2], line.split(",")[3])).collect()
3
4
#loading the RDD object into a dataframe and assigning column names
5
df1 = sqlContext.createDataFrame(rdd1, ['column1', 'column2', 'column3', 'column4']).collect()
6
7
#dropping duplicates from the dataframe
8
df1.dropDuplicates().show()
9
Advertisement
Answer
It is not an import problem. You simply call .dropDuplicates()
on a wrong object. While class of sqlContext.createDataFrame(rdd1, ...)
is pyspark.sql.dataframe.DataFrame
, after you apply .collect()
it is a plain Python list
, and lists don’t provide dropDuplicates
method. What you want is something like this:
JavaScript
1
6
1
(df1 = sqlContext
2
.createDataFrame(rdd1, ['column1', 'column2', 'column3', 'column4'])
3
.dropDuplicates())
4
5
df1.collect()
6