gpt4 book ai didi

excel - 在 pyspark 中以第 3 行作为标题读取 excel 文件

转载 作者:行者123 更新时间:2023-12-04 19:48:58 26 4
gpt4 key购买 nike

我想将读取的 excel 文件作为 spark 数据帧读取,第 3 行作为标题。将 excel 文件读取为 spark 数据帧,第 1 行作为标题的语法是:

s_df  = spark.read.format("com.crealytics.spark.excel") \
.option("header", "true") \
.option("inferSchema", "true") \
.load(path + 'Sales.xlsx')
和以第三行作为标题的 Pandas 数据帧读取的等效语法是:
p_df = pd.read_excel(path + 'Sales.xlsx',header=3)
我想在 pyspark 中做同样的事情,即读取 excel 文件作为 spark 数据帧,第三行作为标题。

最佳答案

使用 dataAddress 选项指定数据所在的单元格/行。由于您需要跳过两行,因此您的数据(包括标题)从 A3 行开始。

s_df = spark.read.format("com.crealytics.spark.excel") \
.option("header", "true") \
.option("inferSchema","true") \
.option("dataAddress", "'Sheet1'!A3") \
.load("yourfilepath")
另外,请注意,如果您的前两行为空,则不必指定 dataAddress。默认情况下将跳过前导空行。
查看文档 here

关于excel - 在 pyspark 中以第 3 行作为标题读取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66989649/

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