Load a workbook tab
Importing a single worksheet for analysis.
Pandas · Function
Reads supported Excel or OpenDocument spreadsheet files into a DataFrame, from a local path, URL, bytes-like input, or file-like object. It can read one sheet or multiple sheets and offers controls for columns, types, missing values, dates, and parsing.
pandas.read_excel opens a supported Excel or OpenDocument file from a path, URL, bytes object, or file-like input and returns tabular data as a DataFrame. The selected sheet, header interpretation, columns, types, missing-value handling, date parsing, and backend can be controlled with keyword arguments.
pandas.read_excel(io, sheet_name=0, *, header=0, names=None, index_col=None, usecols=None, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, parse_dates=False, date_format=None, thousands=None, decimal='.', comment=None, skipfooter=0, storage_options=None, dtype_backend=<no_default>, engine_kwargs=None)| Name | Type | Description |
|---|---|---|
io | str, bytes, ExcelFile, xlrd.Book, path object, or file-like object | Excel file path, URL, bytes-like object, ExcelFile, xlrd.Book, path-like object, or file-like object. |
sheet_name | str, int, list, or None; default 0 | Worksheet name, zero-based worksheet index, list of names or indices, or None for all worksheets. |
header | int, list of int, or None; default 0 | Row containing column labels, row numbers to skip, or None for no header. |
names | array-like; default None | Column labels to use instead of labels read from the file. |
index_col | int, list of int, or None; default None | Column or columns to use as the DataFrame index. |
usecols | int, str, list-like, or callable; default None | Columns to parse, specified by labels, positions, ranges, a list-like object, or callable. |
dtype | Type name or dict of column -> type; default None | Data type for columns, or a column-to-type mapping. |
engine | str or None; default None | Parser engine; when omitted, pandas chooses based on the file extension. |
converters | dict; default None | Per-column conversion functions. |
true_values | list; default None | Values interpreted as true. |
false_values | list; default None | Values interpreted as false. |
skiprows | list-like, int, or callable; default None | Rows to skip, or a callable deciding which rows to skip. |
DataFrame or dict of DataFrames, depending on whether one or multiple sheets are read.
import pandas as pd
from io import BytesIO
# In practice, replace this with an .xlsx path or URL.
with pd.ExcelWriter("sales.xlsx") as writer:
pd.DataFrame({"region": ["East", "West"], "amount": [10, 20]}).to_excel(writer, index=False)
df = pd.read_excel("sales.xlsx", sheet_name=0, usecols=["region", "amount"])
print(df)Creates a small workbook, then reads the first sheet and selects two columns.
Importing a single worksheet for analysis.
Selecting only relevant fields from a wide spreadsheet.
Combining or comparing several worksheets.
Controlling schema and missing-value interpretation at ingestion.
The default sheet_name=0 reads the first worksheet and returns a DataFrame. Passing a sheet name or index selects one worksheet, while a list selects multiple sheets and returns a dictionary of DataFrames.
Follow-up: How would you inspect every worksheet? Pass sheet_name=None; the result is a dictionary keyed by worksheet names.
usecols limits which worksheet columns are imported. It can accept column labels, Excel-style ranges, integer positions, a list-like selection, or a callable evaluated against column names.
Follow-up: How can you select columns by a naming rule? Supply a callable that returns true for the desired column labels.
header identifies the row containing column labels, with 0 meaning the first row by default. names supplies replacement labels, and index_col chooses a column or columns for the resulting index.
Follow-up: How would you prevent an unwanted index column from becoming the index? Leave index_col=None, which is the default.
pandas.ExcelFilepandas.read_csvpandas.DataFrame.to_excel