adil blanco
adil blanco

Reputation: 355

How to save dataframe to pickle file using Pyspark

I have to save a dataframe to Pickle file, but it returns an error

df.saveAsPickleFile(path)

AttributeError: 'Dataframe' object has no attribute 'saveAsPickleFile'

Upvotes: 8

Views: 21915

Answers (1)

Omri374
Omri374

Reputation: 2783

saveAsPickleFile is a method of RDD and not of a data frame.

see this documentation: http://spark.apache.org/docs/latest/api/python/pyspark.html?highlight=pickle

So you can just call:

df.rdd.saveAsPickleFile(filename)

To load it from file, run:

pickleRdd = sc.pickleFile(filename).collect()
df2 = spark.createDataFrame(pickleRdd)

Upvotes: 18

Related Questions