Read row labels
Inspect row identity before label-based selection or alignment.
Pandas · Pipe
DataFrame.index is the DataFrame’s row-label index. Read it to inspect labels or assign a replacement sequence to change them.
DataFrame.index exposes the row-label Index for a DataFrame. Read it to inspect labels, or assign a same-length sequence to replace those labels; the labels then participate in label-based access and alignment.
DataFrame.indexpandas.Index — the index labels of the DataFrame.
df.index represents the labels attached to the DataFrame's rows.import pandas as pd
df = pd.DataFrame(
{"name": ["Alice", "Bob"], "score": [91, 84]},
index=[101, 102],
)
print(df.index)
df.index = [201, 202]
print(df)The property exposes the current row labels and accepts a replacement sequence with one label per row.
Inspect row identity before label-based selection or alignment.
Replace default integer labels with stable domain identifiers.
Verify labels after constructing or transforming a DataFrame.
DataFrame.index return?DataFrame.index returns a pandas Index containing the DataFrame's row labels. It is an attribute rather than a method, so it is accessed as df.index.
Follow-up: How would you replace the labels? Assign a sequence of the intended labels to df.index, with one label corresponding to each row.
The index identifies rows, and its labels may be integers, strings, or other hashable objects. The returned object is a pandas Index.
Follow-up: Why can changing the index alter arithmetic results? DataFrame operations align data by labels, so different labels can change which rows correspond.
Read df.index to inspect row labels and assign to it to modify them. The official example changes [10, 20, 30] to [100, 200, 300] by assigning a new label sequence.
Follow-up: When should a column become the index instead? Use a dedicated index-setting operation when the labels should be derived from existing column values rather than manually supplied.
DataFrame.ilocDataFrame.locDataFrame.reindexDataFrame.reset_indexDataFrame.set_index