Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Pipe

df.index

DataFrame.index is the DataFrame’s row-label index. Read it to inspect labels or assign a replacement sequence to change them.

How it works

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.

Syntax

DataFrame.index

Returns

pandas.Index — the index labels of the DataFrame.

Key points

  • df.index represents the labels attached to the DataFrame's rows.
  • Each row is identified through its corresponding index label.
  • Valid labels include integers, strings, and other hashable values.
  • The index supports selecting by labels and aligning data across objects.
  • Use the attribute to inspect the current index or replace it.
  • The property returns the DataFrame's index labels as a pandas Index.

Common mistakes

  • Index labels are not restricted to integers; strings and other hashable values are valid.
  • Changing labels can affect later label-based selection and how this DataFrame aligns with other objects.

Example: Read and replace row labels

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.

When to use it

Read row labels

Inspect row identity before label-based selection or alignment.

Relabel rows

Replace default integer labels with stable domain identifiers.

Validate index state

Verify labels after constructing or transforming a DataFrame.

Interview questions

QWhat does 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.

QWhat kinds of values can DataFrame index labels contain?

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.

QHow do you modify a DataFrame's index?

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.iloc
  • DataFrame.loc
  • DataFrame.reindex
  • DataFrame.reset_index
  • DataFrame.set_index

Source: pandas.DataFrame.index. Checked 2026-10-15.