pyspark中将数据从列表转换为字符串
作者:互联网
初始的DataFrame:
from pyspark.sql.types import StructType, StructField schema = StructType([StructField("uuid",IntegerType(),True),StructField("test_123",ArrayType(StringType(),True),True)]) rdd = sc.parallelize([[1, ["test","test2","test3"]], [2, ["test4","test","test6"]],[3,["test6","test9","t55o"]]]) df = spark.createDataFrame(rdd, schema) df.show() +----+--------------------+ |uuid| test_123| +----+--------------------+ | 1|[test, test2, test3]| | 2|[test4, test, test6]| | 3|[test6, test9, t55o]| +----+--------------------+
使用udf自定义一个连接列表的方法:
from pyspark.sql.functions import udf, col # join是python中自带的方法,用于返回一个以分隔符sep连接各个元素后生成的字符串 join_udf = udf(lambda x: ",".join(x)) df.withColumn("test_123", join_udf(col("test_123"))).show() +----+----------------+ |uuid| test_123| +----+----------------+ | 1|test,test2,test3| | 2|test4,test,test6| | 3|test6,test9,t55o| +----+----------------+
标签:t55o,join,test6,pyspark,列表,test,123,udf,字符串 来源: https://www.cnblogs.com/Ao0216/p/16194505.html