【问题标题】:How can I read headers with .createDataFrame()?如何使用 .createDataFrame() 读取标题?
【发布时间】:2021-04-22 11:57:32
【问题描述】:

我正在从 Google API 读取一些数据。返回的数据如下所示:

print(data)
[['col1', 'col2', 'col3'], ['543', '15197', '49877729'], ['9288', '285985', '29762969'], ['13709', '28146', '94968440'], ['1', '98480', '154381985'], ['586', '137709', '74962864'] ...]

第一行是标题名称。然后我将上述转换为 Spark df,如下所示:

spark_df = (
  spark
  .createDataFrame(data, header=True)
)

这给出了这个错误:

TypeError: createDataFrame() got an unexpected keyword argument 'header'

我该如何解决这个问题?我在这里查看了接受的答案How to Set Pyspark Dataframe Headers to another Row?,但肯定可以避免循环吗?第二个答案建议使用 header 参数,但它对我来说是错误的。

【问题讨论】:

    标签: python apache-spark pyspark apache-spark-sql


    【解决方案1】:

    您可以尝试将第一个列表项用作架构(第二个参数),并将列表的其余部分用作数据:

    df = spark.createDataFrame(data[1:], data[0])
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2020-07-07
      • 2022-01-10
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多