我正试图从大约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发布于 2022-12-01 06:12:12
您可以连接DataFrames的二叉树,原因是因为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)https://stackoverflow.com/questions/74637544
复制相似问题