我有一个包含这样数据的专栏:
[[[-77.1082606, 38.935738]] ,Point]
我希望将其拆分为:
column 1 column 2 column 3
-77.1082606 38.935738 Point
如何使用PySpark或Scala(Databricks 3.0)?我知道如何爆炸列但不拆分这些结构。感谢!!!
编辑:以下是该列的架构:
|-- geometry: struct (nullable = true)
| |-- coordinates: string (nullable = false)
| |-- type: string (nullable = false
答案 0 :(得分:3)
您可以使用regexp_replace()
删除方括号,然后使用逗号将结果字符串split()
分成不同的列。
from pyspark.sql.functions import regexp_replace, split, col
df.select(regexp_replace(df.geometry.coordinates, "[\[\]]", "").alias("coordinates"),
df.geometry.type.alias("col3")) \
.withColumn("arr", split(col("coordinates"), "\\,")) \
.select(col("arr")[0].alias("col1"),
col("arr")[1].alias("col2"),
"col3") \
.drop("arr") \
.show(truncate = False)
+-----------+----------+-----+
|col1 |col2 |col3 |
+-----------+----------+-----+
|-77.1082606| 38.935738|Point|
+-----------+----------+-----+