【问题标题】:read_excel loop with multiple arguments具有多个参数的 read_excel 循环
【发布时间】:2021-12-25 03:26:13
【问题描述】:

我编写了一个循环来读取目录中的每个 excel 文件并将其附加到数据框。它有效。

file_list = glob.glob(path + "/*.xls")
for file in file_list:
   excl_list.append(pd.read_excel(file))
excl_merged = pd.concat(excl_list, ignore_index=True)

而且,如果我读入一个文件并传入所有我想要的 agruments,那也可以。例如:

df = pd.read_excel('sample.xlsx', usecoles=['code','name','date','hours'],skiprows= [0,1,2,3,4,5,6,7])

但是,如果我尝试将这些相同的参数添加到我的循环中,它就不起作用。有什么建议吗??

 file_list = glob.glob(path + "/*.xls")
    for file in file_list:
       excl_list.append(pd.read_excel(file,usecoles=['code','name','date','hours'],skiprows= [0,1,2,3,4,5,6,7]))
    excl_merged = pd.concat(excl_list, ignore_index=True)

【问题讨论】:

  • 第三个例子发生了什么?有错误吗?你所有的电子表格都有这 4 列吗?
  • “它不起作用”是什么意思?你必须更具体。您应该创建一个minimal reproducible example,以便我们了解发生了什么。
  • 很确定你在usecol 中有错字 e s

标签: python pandas loops


【解决方案1】:
file_list = glob.glob(path + "/*.xls")

excl_merged = pd.concat(
                  [pd.read_excel(file,
                          # usecols, not usecoles
                          usecols=['code','name','date','hours'],
                          skiprows= [0,1,2,3,4,5,6,7])
                      for file in file_list
                  ],
                  ignore_index=True,
              )

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2022-06-16
    • 2014-01-07
    • 2014-10-01
    相关资源
    最近更新 更多