Create PySpark DataFrame from list of tuples
In this article, we are going to discuss the creation of a Pyspark dataframe from a list of tuples.
To do this, we will use the createDataFrame() method from pyspark. This method creates a dataframe from RDD, list or Pandas Dataframe. Here data will be the list of tuples and columns will be a list of column names.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Syntax:
dataframe = spark.createDataFrame(data, columns)
Example 1:
Python3
# importing moduleimport pyspark # importing sparksession from # pyspark.sql modulefrom pyspark.sql import SparkSession # creating sparksession and giving# an app namespark = SparkSession.builder.appName('sparkdf').getOrCreate() # list of tuples of college datadata = [("sravan", "IT", 80), ("jyothika", "CSE", 85), ("harsha", "ECE", 60), ("thanmai", "IT", 65), ("durga", "IT", 91)] # giving column names of dataframecolumns = ["Name", "Branch", "Percentage"] # creating a dataframedataframe = spark.createDataFrame(data, columns) # show data framedataframe.show() |
Output:
Example 2:
Python3
# importing moduleimport pyspark # importing sparksession from # pyspark.sql modulefrom pyspark.sql import SparkSession # creating sparksession and giving # an app namespark = SparkSession.builder.appName('sparkdf').getOrCreate() # list of tuples of plants datadata = [("mango", "AP", "Guntur"), ("mango", "AP", "Chittor"), ("sugar cane", "AP", "amaravathi"), ("paddy", "TS", "adilabad"), ("wheat", "AP", "nellore")] # giving column names of dataframecolumns = ["Crop Name", "State", "District"] # creating a dataframedataframe = spark.createDataFrame(data, columns) # show data framedataframe.show() |
Output:
Example 3:
Python code to count the records (tuples) in the list
Python3
# importing moduleimport pyspark # importing sparksession from# pyspark.sql modulefrom pyspark.sql import SparkSession # creating sparksession and giving# an app namespark = SparkSession.builder.appName('sparkdf').getOrCreate() #list of tuples of plants datadata = [("mango", "AP", "Guntur"), ("mango", "AP", "Chittor"), ("sugar cane", "AP", "amaravathi"), ("paddy", "TS", "adilabad"), ("wheat", "AP", "nellore")] # giving column names of dataframecolumns = ["Crop Name", "State", "District"] # creating a dataframe dataframe = spark.createDataFrame(data, columns) #count records in the listdataframe.count() |
Output:
5


