我希望将 PySpark 数据帧的多列合并到StructType
.
假设我有一个像这样的数据框:
columns = ['id', 'dogs', 'cats']
vals = [(1, 2, 0),(2, 0, 1)]
df = sqlContext.createDataFrame(vals, columns)
我希望生成的数据框与此类似(不是因为它实际上会被打印,而是为了让您了解我的意思,如果您还不熟悉 StructType):
id | animals
1 | dogs=2, cats=0
2 | dogs=0, cats=1
现在我可以通过这样实现我想要的:
StructType(
[StructField('dogs', IntegerType(), True),
[StructField('cats', IntegerType(), True)
)
在我的最后udf
然而,我宁愿只用一个函数来完成它。如果不存在的话我会感到惊讶。