我试图从大约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
1条答案
按热度按时间oewdyzsn1#
您可以concat字典的DataFrames,原因是因为多个sheetnames在
list_of_sheetnames
: