我有一个包含StringType列的数据帧。我需要连接3列,并将结果放在不同的列中。我像这样使用concat_ws:
from pyspark.context import SparkContext
from pyspark.sql import functions as f
def myConcat(*cols):
return f.trim(f.concat_ws('-', *[f.coalesce(c, f.lit("")) for c in cols]))
df = df.withColumn('Column1', myConcat(df['Column2'], df['Column3'], df['Column4'])).show()
这是预期的结果:
+--------------+-----------+---------+--------+
|Column1 |Column2 |Column3 |Column4 |
+--------------+-----------+---------+--------+
| abcd-efg-hij | abcd | efg| hij |
| s675-klm | s675 | | klm |
+--------------+-----------+---------+--------+
然而,一些列是空的,当我运行上面的脚本时,我得到了类似于以下内容:
+--------------+-----------+---------+--------+
|Column1 |Column2 |Column3 |Column4 |
+--------------+-----------+---------+--------+
| abcd-efg-hij | abcd | efg| hij |
| s675--klm | s675 | | klm |
+--------------+-----------+---------+--------+
一个双精度--在第二行结果中。
有没有办法避免这种情况,并得到想要的结果?
发布于 2018-08-21 04:04:36
将字符串列中的空字符串替换为NULL应该有效:
import pyspark.sql.functions as f
def myConcat(*cols):
return f.trim(f.concat_ws('-', *[f.when(c != '', c) for c in cols]))
df.withColumn('Column1', myConcat(df['Column2'], df['Column3'], df['Column4'])).show()
#+-------+-------+-------+------------+
#|Column2|Column3|Column4| Column1|
#+-------+-------+-------+------------+
#| abcd| efg| hij|abcd-efg-hij|
#| s675| | klm| s675-klm|
#+-------+-------+-------+------------+
https://stackoverflow.com/questions/51937668
复制相似问题