2

I have an Excel workbook that I have already loaded and put all the sheets together, now I would like to add a column where I have the name of each original sheet, I don't know if I have to do it before I put everything together, and if that's how I could do it , I am using pandas. This is my code so far, I want the sheet name or number to be in the "Week" column.

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
1
  • >I would like to add a column where I have the name of each original sheet; If i didn't misunderstood the question, just grab the sheet names and do estado.Week = <all_sheet_names_list>? Commented Jun 21, 2020 at 4:07

1 Answer 1

4

This should work:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

The answer of "Ji Wei" worked perfectly for me, thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.