Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Convert column as array to column as string before saving to csv

# In this example, change the field column_as_array to column_as_string before saving.

from pyspark.sql.functions import udf
from pyspark.sql.types import StringType

def array_to_string(my_list):
    return '[' + ','.join([str(elem) for elem in my_list]) + ']'

array_to_string_udf = udf(array_to_string, StringType())

df = df.withColumn('column_as_str', array_to_string_udf(df["column_as_array"]))

# Then you can drop the old column (array type) before saving.
df.drop("column_as_array").write.csv(...)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Convert #column #array #column #string #saving #csv
ADD COMMENT
Topic
Name
3+8 =