在 PySpark 中从字典构建一行
在本文中,我们将讨论如何在 PySpark 中从字典中构建一行
为此,我们将字典传递给 Row() 方法。
句法:
Syntax: Row(dict)
示例 1:使用键值对(字典)作为参数构建一行。
在这里,我们将通过字典传递行
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)
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)
输出:
示例 2:使用字典构建两行的Python程序。
Syntax: Row(dict, dict)
代码:
蟒蛇3
# 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)
输出:
[