PySpark:从列的有序连接创建列

2024-10-03 13:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从pyspark数据帧上两个现有列的有序连接创建新列时遇到问题,即:

+------+------+--------+
| Col1 | Col2 | NewCol |
+------+------+--------+
| ORD  | DFW  | DFWORD |
| CUN  | MCI  | CUNMCI |
| LAX  | JFK  | JFKLAX |
+------+------+--------+

换句话说,我想获取Col1和Col2,按字母顺序对它们进行排序并将它们连接起来。在

有什么建议吗?在


Tags: 数据pysparkcol2col1有序ordlaxdfw
1条回答
网友
1楼 · 发布于 2024-10-03 13:31:00

组合^{cd1>}、^{cd2>}和^{cd3>}

from pyspark.sql.functions import concat_ws, array, sort_array

df = spark.createDataFrame(
    [("ORD", "DFW"), ("CUN", "MCI"), ("LAX", "JFK")],
    ("Col1", "Col2"))

df.withColumn("NewCol", concat_ws("", sort_array(array("Col1", "Col2")))).show()
# +  +  +   +        
# |Col1|Col2|NewCol|
# +  +  +   +
# | ORD| DFW|DFWORD|
# | CUN| MCI|CUNMCI|
# | LAX| JFK|JFKLAX|
# +  +  +   +

相关问题 更多 >