1

This is what i try

from pathlib import Path
loc = Path('D:\DataSciSpec\Practice\Forloopindict.xlsx')
dict = pd.read_excel(loc,sheetname = None)

for i in dict.keys():
    print(i)

I get the name of sheets

Sheet4
Sheet3
Sheet2
Sheet1

I can also see the sheet content one by one

for i in dict.keys():
    print(dict[i].head())

But how put this data in n data frames (equal to no of sheets) and then append one to another

1 Answer 1

2

This will create a single dataframe (df_full) with the data from all sheets.

import pandas as pd

loc = r'D:\DataSciSpec\Practice\Forloopindict.xlsx'
workbook = pd.read_excel(loc,sheet_name = None)
df_full = pd.DataFrame()
for _, sheet in workbook.items():
    df_full = df_full.append(sheet)

# Reset index or you'll have duplicates
df_full = df_full.reset_index(drop=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think this would constitute a separate answer, because I'd just be copy-pasting a lot of your code. That said, for _, sheet in workbook.items(): df_full = df_full.append(sheet) would be the most normal, and possibly slightly more efficient, way of doing it.
Thanks a ton ! learnt that "_" is a variable that is not used

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.