Search
 
SCRIPT & CODE EXAMPLE
 

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(...)
Comment

PREVIOUS NEXT
Code Example
Python :: create new python environment check 
Python :: python substring from end 
Python :: python cut string to length 
Python :: python count of letters in string 
Python :: python 3.11 release date 
Python :: os chdir python 
Python :: nonlocal keyword python 
Python :: matplotlib savefig legend cut off 
Python :: python inject into process 
Python :: django templates 
Python :: counter +1 python 
Python :: python get env 
Python :: gzip folder python 
Python :: pandas dataframe sort by column 
Python :: save model pytorch 
Python :: python lists tuples sets dictionaries 
Python :: global in python 
Python :: python unittest discover 
Python :: pretty printing using rich library in python 
Python :: access class variable from another class python 
Python :: Python Frozenset operations 
Python :: sentence transformers 
Python :: all the symbols on a keyboard python list 
Python :: how to detect the reaction to a message discord.py 
Python :: how to make your own range function in python 
Python :: read parquet from s3 and convert to dataframe 
Python :: Comparison of two csv file and output with differences? 
Python :: python swarm plot seaborn 
Python :: selenium python find element by class name with space 
Python :: Create chatbot in Python - Source: NAYCode.com 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =