Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert pandas dataframe to spark dataframe

import pandas as pd
from pyspark.sql import SparkSession

filename = <'path to file'>
spark = SparkSession.build.appName('pandasToSpark').getOrCreate()
# Assuming file is csv
pandas_df = pd.read_csv(filename)
spark_df = spark.CreateDataFrame(pandas_df)
Comment

dataframe pandas to spark


from pyspark.sql import SparkSession
#Create PySpark SparkSession
spark = SparkSession.builder 
    .master("local[1]") 
    .appName("SparkByExamples.com") 
    .getOrCreate()
#Create PySpark DataFrame from Pandas
sparkDF=spark.createDataFrame(pandasDF) 
sparkDF.printSchema()
sparkDF.show()

#Outputs below schema & DataFrame

root
 |-- Name: string (nullable = true)
 |-- Age: long (nullable = true)

+------+---+
|  Name|Age|
+------+---+
| Scott| 50|
|  Jeff| 45|
|Thomas| 54|
|   Ann| 34|
+------+---+
Comment

spark df to pandas df

some_df = sc.parallelize([
 ("A", "no"),
 ("B", "yes"),
 ("B", "yes"),
 ("B", "no")]
 ).toDF(["user_id", "phone_number"])
pandas_df = some_df.toPandas()
Comment

convert spark dataframe to pandas

# Convert Spark DataFrame back to a Pandas DataFrame using Arrow
pandasDF = sparkDF.select("*").toPandas()
Comment

PREVIOUS NEXT
Code Example
Python :: ValueError: numpy.ndarray size changed 
Python :: dataframe column contains string 
Python :: django user form 
Python :: python add month datetime 
Python :: what is self in programming 
Python :: argparse 
Python :: numpy get index of nan 
Python :: split string every n characters python 
Python :: tkinter give button 2 commands 
Python :: open image in numpy 
Python :: how to get the size of an object in python 
Python :: python pyautogui how to change the screenshot location 
Python :: install re package python 
Python :: how to count docx pages python 
Python :: how to hit enter in selenium python 
Python :: python cv2 screen capture 
Python :: comment dériver une classe python 
Python :: select closest number in array python 
Python :: install models python 
Python :: pip install arcpy python 3 
Python :: search string array python 
Python :: bgr2gray opencv 
Python :: python get ip from hostname 
Python :: check corently installed epython version 
Python :: update jupyter notebook 
Python :: remove stopwords 
Python :: fill missing values in column pandas with mean 
Python :: python pil resize image 
Python :: np array to df 
Python :: next prime number in python 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =