First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
0

How to Read Multiple Sheets to Pandas Dataframe

asked Mar 31 '2

Moha jbr gravatar image

updated Mar 31 '2

jconto gravatar image

HELLO evreyone i'm traying to create a dataframe from un excel with multiple sheets, i want to use one sheets from excel file in some analysis, i try everthings that i know but it does'n work.

##sheets = pd.read_excel('file_excel.xlsx', sheet_name = ['Hourly Market Data'])

1 answer

Sort by » oldest newest most voted
0

answered Mar 31 '2

jconto gravatar image

updated Mar 31 '2

From the pandas.read_excel 'online' manual,

sheet_name str, int, list, or None, default 0
Strings are used for sheet names. Integers are used in zero-indexed sheet positions (chart sheets do not count as a sheet position). Lists of strings/integers are used to request multiple sheets. Specify None to get all worksheets.

Available cases:
Defaults to 0: 1st sheet as a DataFrame
1: 2nd sheet as a DataFrame
"Sheet1": Load sheet with name Sheet1
[0, 1, "Sheet5"]: Load first, second and sheet named Sheet5 as a dict of DataFrame
None: All worksheets.

To read a sheet into a dataframe, try the following:

import os    
import pandas as pd
cwd = os.getcwd()
xlsfile = 'file_excel.xlsx'
df = pd.read_excel(os.path.join(cwd,xlsfile), sheet_name = 'Hourly Market Data')
print(df.head())

To read all sheets into a dictionary of dataframe, try the following:

cwd = os.getcwd()
xlsfile = 'file_excel.xlsx'
dict_df = pd.read_excel(os.path.join(cwd,xlsfile), sheet_name = None)
for key in dict_df:
    print(key)
    print(dict_df[key].head())
link

Comments

thank you for your time. I try it but it didn't work, i get only the first sheet, i try everythings in sheet_name. @jconto

Moha jbr gravatar imageMoha jbr (Mar 31 '2)

Check my updated ansawer.

jconto gravatar imagejconto (Mar 31 '2)

hello I try the updated bus it's the same results, i use None, it's recover the sheet number 1 all time.

Moha jbr gravatar imageMoha jbr (Apr 2 '2)

Did you test the code with another data excel file? is there any error messages during the run?

jconto gravatar imagejconto (Apr 4 '2)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Mar 31 '2

Seen: 1,580 times

Last updated: Mar 31 '22