gpt4 book ai didi

python - 将 PySpark 数据框列从列表转换为字符串

转载 作者:太空狗 更新时间:2023-10-29 19:33:33 28 4
gpt4 key购买 nike

我有这个 PySpark 数据框

+-----------+--------------------+
|uuid | test_123 |
+-----------+--------------------+
| 1 |[test, test2, test3]|
| 2 |[test4, test, test6]|
| 3 |[test6, test9, t55o]|

我想将 test_123 列转换成这样:

+-----------+--------------------+
|uuid | test_123 |
+-----------+--------------------+
| 1 |"test,test2,test3" |
| 2 |"test4,test,test6" |
| 3 |"test6,test9,t55o" |

所以从列表变成字符串。

我如何使用 PySpark 做到这一点?

最佳答案

虽然您可以使用 UserDefinedFunction,但它效率很低。相反,最好使用 concat_ws 函数:

from pyspark.sql.functions import concat_ws

df.withColumn("test_123", concat_ws(",", "test_123")).show()
+----+----------------+
|uuid| test_123|
+----+----------------+
| 1|test,test2,test3|
| 2|test4,test,test6|
| 3|test6,test9,t55o|
+----+----------------+

关于python - 将 PySpark 数据框列从列表转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108331/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com