Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

loop through a list of excel sheets and create different dataframes

Basically, I have an excel file which I have to read all the sheets apart from the first two. I have tried using a list on the sheet_name parameter, however it returns as invalid sintax. Also tried using this

sheets_dict = pd.read_excel('South America services data.xlsx', sheet_name=None)

all_sheets = []
for name, sheet in sheets_dict.items():
    sheet['sheet'] = name
    sheet = sheet.rename(columns=lambda x: x.split('\n')[-1])
    all_sheets.append(sheet)

full_table = pd.concat(all_sheets)
full_table.reset_index(inplace=True, drop=True)

However, I would like to merge them.

I tried using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sheets_dict = pd.read_excel('South America services data.xlsx', sheet_name=([2:]))

But it gives me a invalid sintax error.
This is the error

File "C:\Users\Windows\AppData\Local\Temp\ipykernel_4036\1764796534.py", line 1
    sheets_dict = pd.read_excel('South America services data.xlsx', sheet_name=([2:]))
                                                                                  ^
SyntaxError: invalid syntax

>Solution :

Try using pd.ExcelFile:

xl = pd.ExcelFile("South America services data.xlsx")

all_sheets = []
for i , name in enumerate(xl.sheet_names):

   if i>=2: # Start reading from the third sheet
       sheet = xl.parse(name)
       sheet['sheet'] = name
       sheet = sheet.rename(columns=lambda x: x.split('\n')[-1])
       all_sheets.append(sheet)

full_table = pd.concat(all_sheets)
full_table.reset_index(inplace=True, drop=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading