0 votes
1 view
in Big Data Hadoop & Spark by (11.5k points)

I have a resulting RDD labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions). This has output in this format:

[(0.0, 0.08482142857142858), (0.0, 0.11442786069651742),.....]

What I want is to create a CSV file with one column for labels (the first part of the tuple in above output) and one for predictions(second part of tuple output). But I don't know how to write to a CSV file in Spark using Python.

How can I create a CSV file with the above output?

1 Answer

0 votes
by (31.4k points)

Just map the lines of the RDD (labelsAndPredictions) into strings (the lines of the CSV) then use rdd.saveAsTextFile().

def toCSVLine(data):

  return ','.join(str(d) for d in data)

lines = labelsAndPredictions.map(toCSVLine)


Welcome to Intellipaat Community. Get your technical queries answered by top developers !