user5409172
user5409172

Reputation:

pandas.read_excel parameter "sheet_name" not working

According to pandas doc for 0.21+, pandas.read_excel has a parameter sheet_name that allows specifying which sheet is read. But when I am trying to read the second sheet from an excel file, no matter how I set the parameter (sheet_name = 1, sheet_name = 'Sheet2'), the dataframe always shows the first sheet, and passing a list of indices (sheet_name = [0, 1]) does not return a dictionary of dataframes but still the first sheet. What might be the problem here?

Upvotes: 19

Views: 94032

Answers (9)

Juan Carlos Duarte
Juan Carlos Duarte

Reputation: 1

Try running your code editor as an administrator, that worked for me

Upvotes: 0

Above discussions are great to explore, I was experiencing the same issue with pandas 2.2.2 and this piece of code solved the problem in my case and is reliable to use as well.

import pandas as pd
from pathlib import Path


# let's load the data
data_path:Path = Path('path_to_your_workbook.xlsx')
# sheet_name=[0,1,...] -> index of the sheet names in your workbook
df = pd.read_excel(data_path,sheet_name=[0,1]) #-> will return a dict object
df.get(0) # -> will return the exact dataframe object for that index

Source: https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html

Upvotes: 0

Darkside
Darkside

Reputation: 1

pip install openpyxl

The above command worked for me.

Upvotes: 0

zheng yin
zheng yin

Reputation: 1

I would just use double quotes like this.

# Returns a DataFrame
pd.read_excel("path_to_file.xls", sheet_name="Sheet1")

Upvotes: 0

I also faced this problem until I found this solution:

rd=pd.read_excel(excel_file,sheet_name=['Sheet2']),

Here excel_file means "file name".

The filename should be the full path to the file.

Make sure to use two backslashes (\\) instead of just one!

In my case, this works.

Upvotes: 0

Lee Li Fong
Lee Li Fong

Reputation: 259

Try at Terminal, type the following first, then re-run your program:

pip install xlrd

Upvotes: 1

Torolito
Torolito

Reputation: 33

This works:

df = pd.read_excel(open(file_path_name), 'rb'), sheetname = sheet_name)

file_path_name = your file
sheet_name = your sheet name

This does not for me:

df = pd.read_excel(open(file_path_name), 'rb'), sheet_name = sheet_name)

Gave me only the first sheet, no matter how I defined sheet_name.

--> it is an known error: https://github.com/pandas-dev/pandas/issues/17107

Upvotes: 3

Sergey Solod
Sergey Solod

Reputation: 743

It looks like you're using the old version of Python. So try to change your code

df = pd.read_excel(file_with_data, sheetname=sheet_with_data)

It should work properly.

Upvotes: 24

whiplassh
whiplassh

Reputation: 143

You can try to use pd.ExcelFile:

xls = pd.ExcelFile('path_to_file.xls')
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')

Upvotes: 7

Related Questions