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

Pandas · Module

df.set_index

Sets DataFrame row labels from one or more existing columns or same-length arrays. It can replace the current index or add levels to it.

How it works

Call this method with one column or array to create a regular index, or with multiple keys to create a MultiIndex. By default, selected columns are removed from the returned DataFrame; use append to retain the existing index and inplace to modify the object instead of returning a new DataFrame.

Syntax

DataFrame.set_index(*keys, ***, drop=True, append=False, inplace=False, verify_integrity=False)

Parameters

NameTypeDescription
keyslabel or array-like or list of labels/arraysColumn label, array-like value, or list combining labels and arrays. Arrays must match the DataFrame length.
dropbool, default TrueRemove the columns used to form the new index.
appendbool, default FalseAdd the new levels to the existing index.
inplacebool, default FalseModify the DataFrame instead of creating a new one.
verify_integritybool, default FalseCheck the new index for duplicate labels.

Returns

DataFrame or None: changed row labels, or None when inplace=True.

Key points

  • Use a column label to promote an existing column into the DataFrame’s row index.
  • Keys may be a single label, an array-like object, or a list mixing labels and arrays.
  • The operation either replaces the current index or extends it when append=True.
  • append=True adds the new index levels to the current index, enabling hierarchical labels.
  • verify_integrity=True validates that the resulting index contains no duplicate labels.
  • Leaving duplicate validation disabled is faster when uniqueness does not need to be checked immediately.

Common mistakes

  • With verify_integrity=False, duplicate-index checking is deferred, so explicitly validate uniqueness later when duplicate labels would be invalid.
  • When inplace=True, the method returns None rather than the changed DataFrame.
  • The selected columns disappear from the result by default; set drop=False when they must remain available as columns.

Example: Create a MultiIndex from columns

import pandas as pd

df = pd.DataFrame({
    "year": [2024, 2024, 2025],
    "month": [1, 2, 1],
    "sales": [10, 20, 30],
})

result = df.set_index(["year", "month"])
print(result)

Creates a MultiIndex from the year and month columns while leaving the original DataFrame unchanged.

When to use it

Promote an identifier column to the index

Use a meaningful identifier column as row labels for selection and alignment.

Build a MultiIndex

Combine several columns into hierarchical row labels.

Extend an existing index

Add grouping dimensions to an already indexed DataFrame.

Interview questions

QWhat inputs can keys accept?

The method builds row labels from one or more existing column names or same-length array-like values. One key produces an Index, while multiple keys can produce a MultiIndex.

Follow-up: How can an external sequence participate? Pass it in the keys list; it must have the same length as the DataFrame.

QWhat does drop control?

drop defaults to True, so columns used as index levels are removed from the resulting DataFrame. Set drop=False to retain them as ordinary columns.

Follow-up: What changes when append=True? The supplied keys are added to the current index rather than replacing it.

QHow does inplace affect the result?

By default, the method returns a DataFrame with changed row labels and does not mutate the caller. With inplace=True, it changes the DataFrame and returns None.

Follow-up: How can duplicate labels be checked during the operation? Set verify_integrity=True, accepting the additional validation cost.

  • DataFrame.reset_index
  • DataFrame.reindex

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