首页 > 解决方案 > 在excel中将工作表名称添加到自负的最终合并工作表

问题描述

我想将多个 excel 工作表合并为一个,并使用原始工作表的名称创建一个新列,我使用以下代码:

list_of_sheets = list(df.keys())
cdf = pd.concat(df[sheet] for sheet in list_of_sheets)

# tried 
cdf = pd.concat(df[sheet]["Brand"] for sheet in list_of_sheets)

# and
list_of_sheets = list(df.keys())
for sheet in list_of_sheets:
    df[sheet]["Brand"] = sheet
    cdf = pd.concat(df[sheet])

但它们都不起作用

标签: pythonpandas

解决方案


这能实现你想要的吗?

import pandas as pd

pd.concat(pd.read_excel("my_excel_file.xlsx", sheet_name=None))

工作表的名称将是数据框的索引。


推荐阅读