This Python pandas read Excel worksheet code snippet example helps you read an Excel worksheet to a DataFrame with Python pandas.
Table of Contents
Python pandas Read Excel Worksheet Code Snippet Template
# Source: https://powerspreadsheets.com/
# More information: https://powerspreadsheets.com/python-read-excel-worksheet/
import pandas as pd
pd.read_excel(WorkbookPath,
sheet_name=WorksheetNameOrIndex)
For these purposes:
- WorkbookPath is (in the context of this Python pandas read Excel worksheet code snippet example) a string specifying the Excel workbook where the worksheet you read is stored.
- WorksheetNameOrIndex is one of the following:
- A string specifying the name of the worksheet you read; or
- An integer specifying the (zero-indexed) position of the worksheet you read.
Python pandas Read Excel Worksheet Example Code
The Python pandas read Excel worksheet example code below:
- Reads the “Python pandas Read Worksheet” worksheet (sheet_name=”Python pandas Read Worksheet”) in the “Python pandas Read Excel Worksheet by PowerSpreadsheets dot com.xlsx” workbook stored in the same directory as the Python file. The code:
- Skips the worksheet's first 5 rows (skiprows=5); and
- Reads columns A to J (usecols=”A:J”).
- Returns a DataFrame.
# Source: https://powerspreadsheets.com/
# More information: https://powerspreadsheets.com/python-read-excel-worksheet/
# Import pandas
import pandas as pd
# Return DataFrame
# From the "Python pandas Read Worksheet" worksheet
# In the "Python pandas Read Excel Worksheet by PowerSpreadsheets dot com.xlsx" workbook
# Stored in the same directory as this file
# Skip the first 5 rows of the worksheet and read columns A to J
pd.read_excel("Python pandas Read Excel Worksheet by PowerSpreadsheets dot com.xlsx",
sheet_name="Python pandas Read Worksheet",
usecols="A:J",
skiprows=5)
The images at the top of this post show:
- The worksheet I read with the Python pandas read Excel worksheet example code; and
- The Jupyter Notebook where I run the Python pandas read Excel worksheet example code.
Python pandas Read Excel Worksheet Explanation
The pandas.read_excel function reads an Excel file into a pandas DataFrame.
The pandas.read_excel function accepts more than 20 parameters. The Python pandas read Excel worksheet code snippet template I explain above works with the following 2 parameters:
(1) io:
(In the context of this Python pandas read Excel worksheet code snippet example) A string specifying the Excel workbook where the worksheet you read is stored.
You can specify the Excel workbook using one of the following (among others):
-
- The workbook's filename (for example, “Python pandas Read Excel Worksheet.xlsx”), if the workbook is stored in the same directory as your Python file.
- The workbook's full filename (including its path on disk) as a raw string (for example, r”C:\Users\Power Spreadsheets\Python pandas Read Excel Worksheet.xlsx”).
- A URL.
(2) sheet_name.
-
- A string specifying the name of the worksheet you read; or
- An integer specifying the position of the worksheet you read.
- Worksheet indexes are zero-based. The index of the first worksheet in the workbook is 0.
- Ignore chart sheets when calculating the worksheet's index.
If you omit sheet_name, Python reads the workbook's first worksheet.
The Python pandas read Excel worksheet example code above works with 2 additional parameters:
(1) usecols:
The columns (in the Excel worksheet) to be parsed. If you omit usecols, Python parses all worksheet columns by default.
You can specify usecols using one of the following (among others):
-
- A string containing a comma separated list of column letters or column ranges.
- A list of integers indicating the columns to be parsed. Column indexes are zero-based. The index of the worksheet's first column (column A) is 0.
(2) skiprows:
The rows to skip (when reading) at the top of the Excel worksheet.
You can specify skiprows using one of the following (among others):
-
- The number of lines to skip.
- The number of the lines to skip.
More Excel Python Training Materials and Resources
You can find more Excel Python Tutorials (including other Python code snippet examples) in the organized Tutorials Archive: Click here to visit the Archives.
If you work with macros and VBA, and want to save time when working with these tools, you may be interested in AutoMacro. AutoMacro:
- Is an add-in for VBA.
- Installs directly into the Visual Basic Editor (VBE).
Depending on the version, AutoMacro comes loaded with:
- Code generators;
- An extensive code library;
- The ability to create your own code library; and
- Many other helpful time-saving tools and utilities.
Click here to learn more about AutoMacro (affiliate link).