【问题标题】:Error while loading multiple excel sheets into pandas将多个 Excel 工作表加载到熊猫中时出错
【发布时间】:2020-08-27 22:01:23
【问题描述】:

我尝试将带有多个 excel 表的多个 excel 文件加载到 pandas 数据框中,现在我正在运行:

import pandas as pd
import glob

files = glob.glob(r'C:\...\Data\*.xlsx')
dfs = pd.read_excel(f,sheet_name=None) for f in files]
df = pd.concat(dfs, ignore_index=True)

none 参数确保我在每个 excel 中加载所有工作表,但我收到错误:

TypeError: 无法连接 ' 类型的对象,只有 Series 和 Dataframe obj 有效。

我该如何解决这个问题?

【问题讨论】:

    标签: python excel pandas


    【解决方案1】:

    这里有必要在第一个列表理解中加入 DataFrames,因为如果通过 sheet_name=None 获取 DataFrames 的 Orderdict:

    dfs = [pd.concat(pd.read_excel(f,sheet_name=None)) for f in files]
    df = pd.concat(dfs, ignore_index=True)
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 2020-10-13
      • 2019-08-25
      • 2021-03-21
      • 2023-04-06
      • 2020-01-15
      • 2020-02-12
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多