How to open an excel file with multiple sheets in pandas?
Asked Answered
C

4

6

I have an excel file composed of several sheets. I need to load them as separate dataframes individually. What would be a similar function as pd.read_csv("") for this kind of task?

P.S. due to the size I cannot copy and paste individual sheets in excel

Cupule answered 23/7, 2015 at 9:4 Comment(1)
Possible duplicate of Using Pandas to pd.read_excel() for multiple worksheets of the same workbookAragonite
H
11

Use pandas read_excel() method that accepts a sheet_name parameter:

import pandas as pd

df = pd.read_excel(excel_file_path, sheet_name="sheet_name")

Multiple data frames can be loaded by passing in a list. For a more in-depth explanation of how read_excel() works see: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

Humanitarian answered 23/7, 2015 at 9:5 Comment(1)
sheetname= has been replaced with sheet_name=Considered
S
7

If you can't type out each sheet name and want to read whole worksheet try this:

                dfname=pd.ExcelFile('C://full_path.xlsx')
                print(dfname.sheet_names)
                df=pd.read_excel('C://fullpath.xlsx')
                for items in dfname.sheet_names[1:]:
                    dfnew=pd.read_excel(full_path,sheet_name=items)
                    df=pd.concat([df,dfnew])

The thing is that pd.read_excel() can read the very first sheet and rest are unread.So you can use this

Spitball answered 4/7, 2019 at 18:37 Comment(0)
P
1
import pandas
# setting sheet_name = None, reads all sheets into a dict
sheets = pandas.read_excel(filepath, sheet_name=None)  
# i will be the keys in a dictionary object
# the values are the dataframes of each sheet
for i in sheets: 
    print(f"sheet[{i}]")
    print(f"sheet[{i}].columns={sheets[i].columns}")
    for index, row in sheets[i].iterrows():
        print(f"index={index} row={row}")
Platto answered 17/11, 2022 at 20:29 Comment(0)
C
0

exFile = ExcelFile(f) #load file f

data = ExcelFile.parse(exFile) #this creates a dataframe out of the first sheet in file

Cupule answered 23/7, 2015 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.