将多个Excel工作表加载到 pandas 时出错 [英] Error while loading multiple excel sheets into pandas

查看:67
本文介绍了将多个Excel工作表加载到 pandas 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将具有多个Excel工作表的多个Excel文件加载到熊猫数据框中,现在我正在运行:

I try to load multiple excel files with multiple excel sheets into a pandas dataframe, right now I am running:

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中都加载了所有工作表,但出现错误:

The none parameter makes sure that I load all sheets in every excel, but i get the error:

TypeError:无法连接类型为'的对象,只有Seri​​es和Dataframe objs有效.

TypeError: cannot concatenate object of type ', only Series and Dataframe objs are valid.

我该如何解决?

推荐答案

在此处必须将 DataFrame 加入第一个列表理解中,因为如果通过 sheet_name = None 则获得OrderdictDataFrames:

Here is necessary join DataFrames in first list comprehension, because if pass sheet_name=None get Orderdict of DataFrames:

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

这篇关于将多个Excel工作表加载到 pandas 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆