🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.pivot()

AI & DATA SCIENCE // df-pivot

df.pivot() reshapes a DataFrame from a long format into a wide format, turning unique values from one column into new column headers.

Syntax

df.pivot(index=None, columns=None, values=None)

Deep Dive Course

pivot() takes three column references: index becomes the new row labels, columns' unique values become the new column headers, and values fills in the actual data at each resulting position. It requires each index/columns combination to appear at most once in the original data — if the same combination appears more than once, pivot() raises an error, since it has no way to decide which of the duplicate values should occupy that single cell, which is exactly the situation pivot_table() is designed to handle instead, by aggregating duplicates together.

1Understanding df.pivot()

pivot() takes three column references: index becomes the new row labels, columns' unique values become the new column headers, and values fills in the actual data at each resulting position. It requires each index/columns combination to appear at most once in the original data — if the same combination appears more than once, pivot() raises an error, since it has no way to decide which of the duplicate values should occupy that single cell, which is exactly the situation pivot_table() is designed to handle instead, by aggregating duplicates together.

💡

If df.pivot() raises a 'duplicate entries' error, it means your index/columns combination isn't actually unique in the source data — use pd.pivot_table() instead, which aggregates duplicate combinations together, with a function like mean or sum, rather than requiring uniqueness.

editor.html
import pandas as pd

df = pd.DataFrame({"date": ["2026-01-01", "2026-01-01", "2026-01-02"], "city": ["NYC", "LA", "NYC"], "temp": [30, 60, 32]})
print(df.pivot(index="date", columns="city", values="temp"))
localhost:3000

2Practical Example

Here is a real-world application of df.pivot() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"date": ["d1", "d1", "d1"], "city": ["NYC", "NYC", "LA"], "temp": [30, 31, 60]})
df.pivot(index="date", columns="city", values="temp")
localhost:3000

3Best Practices

Follow these guidelines when working with df.pivot():

1. Use pivot() specifically when you're confident each index/columns combination is already unique in your data

2. Reach for pd.pivot_table() instead of pivot() as soon as duplicate combinations are a realistic possibility, since it aggregates rather than erroring

3. Verify the resulting wide-format shape makes sense, expected row and column counts, after a pivot, as a quick sanity check

⚠️

Tip: If df.pivot() raises a 'duplicate entries' error, it means your index/columns combination isn't actually unique in the source data — use pd.pivot_table() instead, which aggregates duplicate combinations together, with a function like mean or sum, rather than requiring uniqueness.

editor.html
import pandas as pd

df = pd.DataFrame({"date": ["2026-01-01", "2026-01-01", "2026-01-02"], "city": ["NYC", "LA", "NYC"], "temp": [30, 60, 32]})
print(df.pivot(index="date", columns="city", values="temp"))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"date": ["2026-01-01", "2026-01-01", "2026-01-02"], "city": ["NYC", "LA", "NYC"], "temp": [30, 60, 32]})
print(df.pivot(index="date", columns="city", values="temp"))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"date": ["d1", "d1", "d1"], "city": ["NYC", "NYC", "LA"], "temp": [30, 31, 60]})
df.pivot(index="date", columns="city", values="temp")

Best Practices

  • Use pivot() specifically when you're confident each index/columns combination is already unique in your data
  • Reach for pd.pivot_table() instead of pivot() as soon as duplicate combinations are a realistic possibility, since it aggregates rather than erroring
  • Verify the resulting wide-format shape makes sense, expected row and column counts, after a pivot, as a quick sanity check

Interview Question

Why does df.pivot() raise an error if the same index/columns combination appears more than once in the source data?

Hint: Think about what pivot() would have to decide, but has no rule for deciding, in that situation.

pivot() maps each unique combination of the index and columns values to exactly one cell in the resulting wide-format table, so it expects at most one value for any given combination in the source data. If that combination appears more than once, with potentially different values each time, pivot() has no built-in rule for deciding which of those duplicate values should actually end up in the single resulting cell, so rather than arbitrarily picking one or silently overwriting, it raises an error. pd.pivot_table() solves exactly this situation by explicitly aggregating duplicate combinations together with a specified function, like taking their mean or sum, instead of requiring uniqueness.

Exercises

MediumPractice using df.pivot() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"date": ["2026-01-01", "2026-01-01", "2026-01-02"], "city": ["NYC", "LA", "NYC"], "temp": [30, 60, 32]})
print(df.pivot(index="date", columns="city", values="temp"))

Frequently Asked Questions

Why does df.pivot() raise an error if the same index/columns combination appears more than once in the source data?

pivot() maps each unique combination of the index and columns values to exactly one cell in the resulting wide-format table, so it expects at most one value for any given combination in the source data. If that combination appears more than once, with potentially different values each time, pivot() has no built-in rule for deciding which of those duplicate values should actually end up in the single resulting cell, so rather than arbitrarily picking one or silently overwriting, it raises an error. pd.pivot_table() solves exactly this situation by explicitly aggregating duplicate combinations together with a specified function, like taking their mean or sum, instead of requiring uniqueness.

Related Functions

pd-pivot-tablepd-meltdf-stack