gpt4 book ai didi

python - 在 Pyspark 中将 Pandas Dataframe 转换为 Spark Dataframe 时出现类型错误

转载 作者:行者123 更新时间:2023-12-01 03:56:14 24 4
gpt4 key购买 nike

我进行了研究,但没有发现任何相关内容。我想将一个简单的 pandas.DataFrame 转换为 Spark 数据帧,如下所示:

df = pd.DataFrame({'col1': ['a', 'b', 'c'], 'col2': [1, 2, 3]})
sc_sql.createDataFrame(df, schema=df.columns.tolist())

我得到的错误是:

TypeError: Can not infer schema for type: <class 'str'>

我尝试了更简单的方法:

df = pd.DataFrame([1, 2, 3])
sc_sql.createDataFrame(df)

我得到:

TypeError: Can not infer schema for type: <class 'numpy.int64'>

有什么帮助吗?是否需要手动指定架构等?

sc_sql 是一个 pyspark.sql.SQLContext,我在 python 3.4 和 Spark 1.6 上的 jupyter 笔记本中。

谢谢!

最佳答案

与您的spark版本有关,spark最新更新使类型推断更加智能。您可以通过添加如下架构来解决此问题:

mySchema = StructType([ StructField("col1", StringType(), True), StructField("col2", IntegerType(), True)])
sc_sql.createDataFrame(df,schema=mySchema)

关于python - 在 Pyspark 中将 Pandas Dataframe 转换为 Spark Dataframe 时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com