我使用带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