数据框中的excel工作表名称

2024-10-04 03:23:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Excel工作簿,我已经加载并将所有工作表放在一起,现在我想添加一个列,其中我有每个原始工作表的名称,我不知道在我将所有内容放在一起之前是否必须这样做,如果我可以这样做,我正在使用熊猫。这是到目前为止我的代码,我希望工作表名称或编号在“周”列中

xlsx= pd.ExcelFile('archivo.xlsx')
hojas=[]
for hojaslibro in xlsx.sheet_names:
    hojas.append(xlsx.parse(hojaslibro))
estado=pd.concat(hojas,ignore_index=True)

estado['Week']=0

Tags: 代码in名称内容forxlsxexcel编号
1条回答
网友
1楼 · 发布于 2024-10-04 03:23:11

这应该起作用:

xl = pd.ExcelFile('archvio.xlsx')
df_combined = pd.DataFrame()
for sheet_name in xl.sheet_names:
    df = xl.parse(sheet_name)
    df['Week'] = sheet_name       # this adds `sheet_name` into the column `Week`
    df_combined = df_combined.append(df)

相关问题 更多 >