我有一个已经加载并将所有工作表放在一起的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
答案 0 :(得分:1)
这应该有效:
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)