How to Read Excel File in Python: Simple Guide
Use the
pandas library to read Excel files in Python with the pandas.read_excel() function. This function loads the Excel file into a DataFrame, which you can then use to access and manipulate the data easily.Syntax
The basic syntax to read an Excel file is:
pandas.read_excel(path, sheet_name=0)
Here, path is the file location, and sheet_name specifies which sheet to read (default is the first sheet).
python
import pandas as pd df = pd.read_excel('file.xlsx', sheet_name=0)
Example
This example shows how to read an Excel file named data.xlsx and print its contents.
python
import pandas as pd df = pd.read_excel('data.xlsx') print(df)
Output
Name Age City
0 John 28 NY
1 Anna 22 LA
2 Mike 32 SF
Common Pitfalls
- Not installing
pandasoropenpyxl(needed for .xlsx files). - Using wrong file path or filename causes
FileNotFoundError. - For older Excel files (.xls), you may need
xlrdlibrary. - Specifying incorrect
sheet_namecan cause errors or empty data.
python
import pandas as pd # Wrong: missing engine for .xlsx file # df = pd.read_excel('data.xlsx', engine='xlrd') # This will fail # Right: use openpyxl engine for .xlsx files df = pd.read_excel('data.xlsx', engine='openpyxl')
Quick Reference
Here is a quick summary of key points when reading Excel files in Python:
| Task | Description |
|---|---|
| Import pandas | import pandas as pd |
| Read Excel file | pd.read_excel('file.xlsx') |
| Specify sheet | sheet_name='Sheet2' or sheet_name=0 |
| Install dependencies | pip install pandas openpyxl |
| Handle .xls files | Use engine='xlrd' (legacy) |
Key Takeaways
Use pandas.read_excel() to load Excel files into DataFrames easily.
Always install pandas and openpyxl to handle modern Excel files (.xlsx).
Check the file path and sheet name to avoid common errors.
For older .xls files, use the xlrd engine but prefer .xlsx format.
Print the DataFrame to see the loaded data and verify success.