【发布时间】:2016-08-20 12:43:11
【问题描述】:
输入
我有一个Parameters 类型的列map,形式为:
>>> from pyspark.sql import SQLContext
>>> sqlContext = SQLContext(sc)
>>> d = [{'Parameters': {'foo': '1', 'bar': '2', 'baz': 'aaa'}}]
>>> df = sqlContext.createDataFrame(d)
>>> df.collect()
[Row(Parameters={'foo': '1', 'bar': '2', 'baz': 'aaa'})]
输出
我想在 pyspark 中对其进行重塑,以便所有键(foo、bar 等)都是列,即:
[Row(foo='1', bar='2', baz='aaa')]
使用withColumn 有效:
(df
.withColumn('foo', df.Parameters['foo'])
.withColumn('bar', df.Parameters['bar'])
.withColumn('baz', df.Parameters['baz'])
.drop('Parameters')
).collect()
但是我需要一个不明确提及列名的解决方案,因为我有几十个。
架构
>>> df.printSchema()
root
|-- Parameters: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
【问题讨论】:
-
想要的输出是什么?
-
@eliasah 刚刚编辑了 Q 以获得所需的输出
标签: python apache-spark dataframe pyspark apache-spark-sql