hadoop 无法创建DataFrame

j8yoct9x  于 2023-11-16  发布在  Hadoop
关注(0)|答案(2)|浏览(193)

我试着运行一个简单的PySpark程序来测试。
下面是我的代码:

if __name__ == "__main__":    
spark = SparkSession.builder \    
    .appName("Welcome Spark") \    
    .master("local[2]") \    
    .getOrCreate()    

data_list = [("Aishwarya", 21),("Jhanavi", 19),("Maithree", 23),];

df = spark.createDataFrame(data_list).toDF("Name", "Age")    
df.show()

字符串
我正试图将列表添加到一个数据框中。创建数据框时出现错误。

data_list = [("Aishwarya", 21),("Jhanavi", 19),("Maithree", 23),];  
df = spark.createDataFrame(data_list).toDF("Name", "Age") 
df.show()

ajsxfq5m

ajsxfq5m1#

这对我很有效

import pyspark
from pyspark.sql import SparkSession

if __name__ == "__main__":
    spark = SparkSession.builder.appName("Welcome Spark").master("local[*]").getOrCreate()
    data_list = [("Aishwarya", 21),("Jhanavi", 19),("Maithree", 23),];
    df = spark.createDataFrame(data_list).toDF("Name", "Age")
    df.show()

字符串

cpjpxq1n

cpjpxq1n2#

你可以尝试以下两种方法,都适用于我:

# option 1

data_list = [("Aishwarya", 21),("Jhanavi", 19),("Maithree", 23),]

new_dfdf = spark.createDataFrame(data_list).toDF("Name", "Age")
new_dfdf.show(3)

# option 2

op_dfdf = spark.createDataFrame(data_list, ("Name", "Age"))
op_dfdf.show(3)

字符串

相关问题