我使用帶PySpark的Jupyter Notebook。在那裏我有一個數據框架,這些數據架構有一個列名和類型(整數,...)的列。現在我使用flatMap這樣的方法,但是這會返回一個沒有固定類型的元組列表。有沒有辦法實現這一點?PySpark平面圖應該返回帶有類型值的元組
df.printSchema()
root
|-- name: string (nullable = true)
|-- ...
|-- ...
|-- ratings: integer (nullable = true)
然後我用flatMap做的額定值一些計算(這裏混淆):
df.flatMap(lambda row: (row.id, 5 if (row.ratings > 5) else row.ratings))
y_rate.toDF().printSchema()
現在我得到一個錯誤:
TypeError: Can not infer schema for type:
有什麼辦法通過保持模式使用map/flatMap/reduce?或者至少返回具有特定類型值的元組?
非常有幫助。感謝您的示例代碼。我只是沒有得到flatMap vs Map的部分。 – Matthias
'flatMap'是一個函數'RDD [T] =>(T => Iterable [U])=> RDD [U]'。換句話說,它期望函數返回'Itereble'(Python元組),並連接這些(變平)結果。 – zero323
有沒有辦法在該聲明中給出when/otherwise列的名稱?請參閱'df.select(df.id,when(df.ratings> 5,5).otherwise(df.ratings))'@ zero323 – Matthias