pandas 将多个工作簿中的特定工作表连接到一个df时出错

afdcj2ne  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(170)

我试图从大约300个excel工作簿中分离出一个特定的工作表,并将它们合并到一个单一的数据框架中。
我试过这个代码:

import pandas as pd
import glob
import openpyxl
from openpyxl import load_workbook

pd.set_option("display.max_rows", 100, "display.max_columns", 100)
allexcelfiles = glob.glob(r"C:\Users\LELI Laptop 5\Desktop\DTP1\*.xlsx")
cefdf = []

for ExcelFile in allexcelfiles:
    wb = load_workbook(ExcelFile)
    for sheet in wb:
        list_of_sheetnames = [sheet for sheet in wb.sheetnames if "SAR" in sheet]
        df = pd.read_excel(ExcelFile, sheet_name = list_of_sheetnames, nrows = 24)
        cefdf.append(df)
df = pd.concat(cefdf)

我得到这个错误:

TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

然后我试了一下:

df = pd.DataFrame(pd.read_excel(ExcelFile, sheet_name = list_of_sheetnames, nrows = 24))

我得到这个错误:

ValueError: If using all scalar values, you must pass an index
oewdyzsn

oewdyzsn1#

您可以concat字典的DataFrames,原因是因为多个sheetnames在list_of_sheetnames

for ExcelFile in allexcelfiles:
    wb = load_workbook(ExcelFile)

    list_of_sheetnames = [sheet for sheet in wb.sheetnames if "SAR" in sheet]
    
    dfs = pd.read_excel(ExcelFile, sheet_name = list_of_sheetnames, nrows = 24)
    cefdf.append(pd.concat(dfs))
    
df = pd.concat(cefdf)

相关问题