Reading an Excel file in pandas takes one line: pd.read_excel("file.xlsx"). That returns a DataFrame of the first sheet. To choose a different sheet, pass sheet_name="Sheet2" or an index like sheet_name=1. To read every sheet at once, use sheet_name=None, which returns a dictionary of DataFrames keyed by sheet name. The rest of this guide covers the setup, the options that matter, and what to do when the import fails.
What You'll Need
- Python 3 installed and working from a terminal or command prompt.
- pandas installed (
pip install pandas). - An Excel engine installed. pandas delegates the actual file parsing to a separate library. For
.xlsxfiles useopenpyxl(pip install openpyxl). For legacy.xlsfiles usexlrd(pip install xlrd). Recent pandas versions no longer bundle these automatically, so install the one that matches your file type. - The Excel file saved locally, ideally in the same folder as your script, so you can reference it by a short relative path.
Step-by-Step
- Install the packages. In a terminal run
pip install pandas openpyxl. If you also need to read old.xlsfiles, addxlrd. If you use a virtual environment, activate it first so the packages land in the right place. - Import pandas. At the top of your script or notebook write
import pandas as pd. Thepdalias is a convention, not a requirement, but nearly every example you will find uses it. - Point at the file. Decide on the path. A relative path like
"data/sales.xlsx"resolves from the folder you run the script in. A raw string withrprefix avoids backslash escaping problems on Windows:r"C:\Users\me\data.xlsx". - Read the first sheet. Call
df = pd.read_excel("data/sales.xlsx"). The result is a DataFrame containing the first worksheet. Printdf.head()to confirm the columns and values look right. - Select a specific sheet. Pass
sheet_name. Use the tab name as a string (sheet_name="Q1") or a zero-based position (sheet_name=0for the first sheet,1for the second). If you are unsure of the names, read withsheet_name=Nonefirst and inspect the dictionary keys. - Read all sheets at once.
sheets = pd.read_excel("data/sales.xlsx", sheet_name=None)gives you a dict such as{"Q1": df1, "Q2": df2}. You can then loop oversheets.items()or pick one by key. - Handle headers and skipped rows. If the real header is not on row 1, use
header=2(zero-based row index) orheader=Noneto get default integer column names. Useskiprows=3to drop preamble rows above the table. Useusecols="A:C"orusecols=[0, 2, 5]to load only the columns you need. - Deal with missing values and types. By default pandas converts blank cells to
NaN. Passna_values=["N/A", "-", ""]to treat extra placeholder strings as missing. Usedtype={"Zip": str}to stop pandas from turning a numeric-looking code into a number and losing leading zeros. - Verify the result. Run
df.shape,df.columnsanddf.dtypes. These three checks catch most import surprises before you start analysis.
Common Mistakes
- Forgetting the engine. Installing pandas but not
openpyxlproduces an ImportError or a message about a missing optional dependency. Install the engine for your file type. - Wrong path. A FileNotFoundError almost always means the working directory is not what you think. Print
os.getcwd()or use an absolute path to confirm. - Assuming the first sheet is the data. Workbooks often open on a cover or summary tab. Check
sheet_name=Noneand pick deliberately. - Losing leading zeros. Part numbers, ZIP codes and phone numbers stored as text can come back as integers. Force string dtype for those columns.
- Header mismatch. Merged cells and title rows above the table push the real header down. Adjust
headerandskiprowsrather than renaming columns after the fact.
Tips
- Read a small sample first with
nrows=100to check layout quickly on a large workbook. - Use
sheet_name=Noneonce to discover exact tab names, then switch to a single-sheet read for speed. - If the file changes often, wrap the read in a function so the path, sheet and dtype settings live in one place.
- For very large workbooks, consider converting to CSV once and reading that with
pd.read_csv, which is generally faster.
Troubleshooting
Work through these in order, simplest first.
- ModuleNotFoundError for openpyxl or xlrd. Install the missing engine with pip, then restart your Python session so the import picks it up.
- FileNotFoundError. Confirm the file exists and the path is correct for your current working directory. Try an absolute path to isolate the issue.
- UnicodeDecodeError or garbled text. This usually points to an old or unusual file format. Re-save the file as
.xlsxfrom Excel and read it again. - Empty DataFrame. Check that the sheet name is correct and that
headerandskiprowsare not skipping past the data. - Columns named Unnamed: 0, Unnamed: 1. The header row was not detected. Set
headerto the correct row index. - Still stuck. Open the file in Excel, confirm the tab names and the first populated row, then match those to your read call.
Frequently Asked Questions
Can pandas read .xls files?
Yes, with the xlrd engine installed. Newer pandas versions require it explicitly for legacy .xls files, while .xlsx uses openpyxl.
How do I read only one column?
Pass usecols=["ColumnName"] or a letter range like usecols="B". This loads just that column and is faster on wide sheets.
Why are my numbers stored as text?
Excel may store them as text, or pandas may have inferred a type you did not expect. Check df.dtypes and pass an explicit dtype mapping when reading.
Can I read a password-protected workbook?
Not directly with a plain read_excel call. Remove the password in Excel first, or use a library that supports decryption, then hand the decrypted file to pandas.
How do I read every sheet in a workbook?
Use sheet_name=None. The result is a dictionary of DataFrames keyed by sheet name, which you can iterate over.
Does read_excel work with Google Sheets?
Only after you download or export the sheet as an .xlsx file. pandas reads local files and URLs, not Google Sheets directly.