【发布时间】:2021-12-22 21:52:42
【问题描述】:
我有以下架构:
>>> df.printSchema()
root
... SNIP ...
|-- foo: array (nullable = true)
| |-- element: struct (containsNull = true)
... SNIP ...
| | |-- value: double (nullable = true)
| | |-- value2: double (nullable = true)
在这种情况下,我在数据框和 foo 数组中只有一行:
>>> df.count()
1
>>> df.select(explode('foo').alias("fooColumn")).count()
1
value 为空:
>>> df.select(explode('foo').alias("fooColumn")).select('fooColumn.value','fooColumn.value2').show()
+-----+------+
|value|value2|
+-----+------+
| null| null|
+-----+------+
我想编辑value 并创建一个新的数据框。我可以爆foo并设置value:
>>> fooUpdated = df.select(explode("foo").alias("fooColumn")).select("fooColumn.*").withColumn('value', lit(10)).select('value').show()
+-----+
|value|
+-----+
| 10|
+-----+
如何折叠此数据框以将 fooUpdated 作为带有 struct 元素的数组重新放入,或者有没有办法在不爆炸 foo 的情况下做到这一点?
最后,我想要以下:
>>> dfUpdated.select(explode('foo').alias("fooColumn")).select('fooColumn.value', 'fooColumn.value2').show()
+-----+------+
|value|value2|
+-----+------+
| 10| null|
+-----+------+
【问题讨论】:
标签: apache-spark pyspark apache-spark-sql