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

Pandas · Module

df.pivot

Reshapes a DataFrame into a matrix using selected fields for its index, columns, and values. It does not aggregate duplicate index-column combinations.

How it works

DataFrame.pivot reshapes long-form data into a matrix by selecting fields for the output index, columns, and cell values. It requires each index-and-column pair to identify one value; use pivot_table when duplicate pairs need aggregation.

Syntax

DataFrame.pivot(*, columns, index=<no_default>, values=<no_default>)

Parameters

NameTypeDescription
columnsHashable or a sequence of the previousColumn to use to make new frame’s columns.
indexHashable or a sequence of the previous, optionalColumn to use to make new frame’s index. If not given, uses existing index.
valuesHashable or a sequence of the previous, optionalColumn(s) to use for populating new frame’s values. If not specified, all remaining columns will be used and the result will have hierarchically indexed columns.

Returns

DataFrame

Key points

  • The method returns a reshaped DataFrame whose organization is determined by the chosen index and column values.
  • Distinct labels from the selected axis fields become the row and column labels of the result.
  • The required columns argument names the field, or fields, that define the new columns.
  • The optional index argument names the field, or fields, that define the new index; without it, the existing index is retained.
  • The optional values argument selects the data placed in the reshaped cells.
  • index, columns, and values accept a single hashable label or a sequence of labels.

Common mistakes

  • pivot does not aggregate duplicate records. If several rows map to the same index-and-column pair, use pivot_table for aggregation; multiple selected value columns instead produce hierarchical output columns.
  • Omitting values includes every remaining column, which can create MultiIndex columns rather than a single value column.

Example: Reshape long-form sales data

import pandas as pd

sales = pd.DataFrame({
    "store": ["A", "A", "B"],
    "quarter": ["Q1", "Q2", "Q1"],
    "revenue": [10, 12, 9],
})

wide = sales.pivot(index="store", columns="quarter", values="revenue")
print(wide)

Uses store values as rows, quarter values as columns, and revenue as the resulting cells.

When to use it

Long to wide reshaping

Convert records with category and measure fields into a wide matrix for inspection or downstream processing.

Index-preserving reshape

Keep the source index as the output row axis while spreading one field across columns.

Multiple-measure output

Expose several measures at once when a single index-column pair maps to multiple value fields.

Interview questions

QWhat does DataFrame.pivot do?

The method reshapes a DataFrame using unique values from the chosen index and columns to form the result axes. The selected values populate the cells.

Follow-up: What happens when a pair is not unique? pivot cannot aggregate it; choose pivot_table when aggregation is required.

QWhich arguments control the output axes?

columns is required and identifies the field whose distinct values become output columns. index is optional; when omitted, the existing DataFrame index supplies the output index.

Follow-up: How can several fields define an axis? Pass a sequence for index or columns, producing a hierarchical axis when appropriate.

QWhat does the values argument control?

values is optional and selects the column or columns used to populate the result. If omitted, all columns not used for index or columns are included, yielding hierarchically indexed columns.

Follow-up: How would you create a summarized cross-tabulation? Use pivot_table and provide an aggregation function.

  • DataFrame.pivot_table
  • pandas.pivot
  • pandas.pivot_table

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