在 PySpark 中,如何将所有列中的字符串拆分为字符串列表?
a = [('a|q|e','d|r|y'),('j|l|f','m|g|j')]
df = sc.createDataFrame(a,['col1','col2'])
+-----+-----+
| col1| col2|
+-----+-----+
|a|q|e|d|r|y|
|j|l|f|m|g|j|
+-----+-----+
预期输出:
+---------+---------+
| col1| col2|
+---------+---------+
|[a, q, e]|[d, r, y]|
|[j, l, f]|[m, g, j]|
+---------+---------+
我可以使用withColumn
一次处理单列,但对于动态数量的列,这不是一个令人满意的解决方案。
from pyspark.sql.functions import col, split
outDF = df.withColumn("col1", split(col("col1"), "\\|").alias("col1"))