Input
我有一个专栏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'})]
df.printSchema()
# root
# |-- Parameters: map (nullable = true)
# | |-- key: string
# | |-- value: string (valueContainsNull = true)
Output
我想在 PySpark 中重塑它,以便所有键 (foo
, bar
等)将成为列,即:
[Row(foo='1', bar='2', baz='aaa')]
Using withColumn
works:
(df
.withColumn('foo', df.Parameters['foo'])
.withColumn('bar', df.Parameters['bar'])
.withColumn('baz', df.Parameters['baz'])
.drop('Parameters')
).collect()
But 我需要一个没有明确提及列名称的解决方案,因为我有几十个。