【问题标题】:TypeError converting a Pandas Dataframe to Spark Dataframe in Pyspark在 Pyspark 中将 Pandas 数据帧转换为 Spark 数据帧的 TypeError
【发布时间】:2016-05-24 09:45:21
【问题描述】:

我进行了研究,但没有找到任何相关信息。我想将一个简单的 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_sqlpyspark.sql.SQLContext,我在 python 3.4 和 spark 1.6 的 jupyter notebook 中。

谢谢!

【问题讨论】:

  • 我试过代码工作正常,没有错误。
  • 它不适合我,不管有没有架构......
  • 您使用的是哪个 spark 版本?
  • 我在 Spark 1.6.1
  • 你用的是什么版本的 Pandas?

标签: python pandas apache-spark pyspark


【解决方案1】:

和你的spark版本有关,最新更新的spark让类型推断更加智能。您可以通过添加这样的架构来解决此问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2021-11-16
    • 2016-09-27
    • 2020-07-24
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多