Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Building a row from a dictionary in PySpark

  • Last Updated : 18 Jul, 2021

In this article, we will discuss how to build a row from the dictionary in PySpark

For doing this, we will pass the dictionary to the Row() method.

 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:



Syntax: Row(dict)

Example 1: Build a row with key-value pair (Dictionary) as arguments.

Here, we are going to pass the Row with Dictionary 

Syntax: Row({‘Key’:”value”, ‘Key’:”value”,’Key’:”value”})

Python3




# import Row
from pyspark.sql import Row
  
# dict
dic = {'First_name':"Sravan"
       'Last_name':"Kumar",
       'address':"hyderabad"}
  
# create a row with three values
# as dictionary.
row = Row(dic)
  
# display row
print(row)

Output:

<Row({‘First_name’: ‘Sravan’, ‘Last_name’: ‘Kumar’, ‘address’: ‘hyderabad’})>

Example 2: Python program to build two Rows with dictionary.

Syntax: Row(dict, dict)

Code:

Python3




# import Row
from pyspark.sql import Row
  
dic_1 = {'First_name':"Sravan"
         'Last_name':"Kumar",
         'address':"hyderabad"}
  
dic_2 = {'First_name':"Bobby",
         'Last_name':"Gottumukkala",
         'address':"Ponnur"}
  
# create two rows with
# three values as dictionary.
row = [Row(dic_1),
       Row(dic_2)]
# display row
print(row)

Output:

[<Row({‘First_name’: ‘Sravan’, ‘Last_name’: ‘Kumar’, ‘address’: ‘hyderabad’})>,

 <Row({‘First_name’: ‘Bobby’, ‘Last_name’: ‘Gottumukkala’, ‘address’: ‘Ponnur’})>]




My Personal Notes arrow_drop_up
Recommended Articles
Page :