Ask Your Question
0

How to Read Multiple Sheets to Pandas Dataframe

asked 2022-03-31 08:54:58 -0600

Moha jbr gravatar image

updated 2022-03-31 11:05:45 -0600

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'])
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2022-03-31 11:18:06 -0600

jconto gravatar image

updated 2022-03-31 15:22:54 -0600

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())
edit flag offensive delete link more

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 ( 2022-03-31 13:47:47 -0600 )edit

Check my updated ansawer.

jconto gravatar imagejconto ( 2022-03-31 15:23:37 -0600 )edit

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 ( 2022-04-02 15:46:53 -0600 )edit

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

jconto gravatar imagejconto ( 2022-04-04 10:04:16 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2022-03-31 08:54:58 -0600

Seen: 1,038 times

Last updated: Mar 31 '22