Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

save dataframe to a csv local file pyspark

df.repartition(1).write.format('com.databricks.spark.csv').save("/path/to/file/myfile.csv",header = 'true')
Comment

write PySpark dataframe 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 :: pandas dataframe column names 
Python :: get flask version 
Python :: pandas sort 
Python :: how to pair up two lists in python 
Python :: print str and float python 
Python :: catch error python 
Python :: generate a random number in python between 0 and 1 
Python :: django models using Value 
Python :: subprocess print logs 
Python :: python restart script 
Python :: flatten numpy array 
Python :: how to load keras model from json 
Python :: How to get the value of an Entry widget in Tkinter? 
Python :: boto signed url 
Python :: python - iterate with the data frame 
Python :: python currency symbol 
Python :: Simple way to measure cell execution time in ipython notebook 
Python :: how to use dictionary comprehension to make a dictionary for some names of a list in python 
Python :: django query field is null 
Python :: how to use regex in a list 
Python :: how to download the captions of a youtube video 
Python :: death stranding 
Python :: find an element in pandas 
Python :: add to number in python 
Python :: lag function in pandas 
Python :: input age in python 
Python :: scanner class in python 
Python :: how to detect if the space button is pressed in pygame 
Python :: Python Tkinter Canvas Widget 
Python :: replace df with 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =