๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

ValueError: Cannot Reindex From a Duplicate Axis โ€” How to Fix It

Answer

This error means your dataframe has duplicate values in its index, and pandas can't perform an operation that requires unique index values. Fix it by resetting the index with .reset_index(drop=True) or removing duplicates with .drop_duplicates() before the operation.

Why This Happens

Certain pandas operations โ€” like assigning a Series to a column, reindexing, or aligning dataframes โ€” require the index to be unique. If your index has duplicate values (often from merges, concats, or filtering without resetting), pandas can't figure out which row should get which value.

Solution

The rule: if you hit this error, run df.index.is_unique โ€” if it returns False, reset or dedupe the index.

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3]}, index=[0, 1, 1])  # duplicate index value

# โŒ Problematic: assigning with duplicate index
df['b'] = pd.Series([10, 20, 30])
# ValueError: cannot reindex from a duplicate axis

# โœ… Fixed: reset the index first
df = df.reset_index(drop=True)
df['b'] = [10, 20, 30]

# โœ… Debug: check for duplicates
print(df.index.is_unique)  # False means duplicates exist
print(df.index[df.index.duplicated()])  # shows which values are duplicated

# โœ… Alternative: if duplicates are data issue, drop them
df = df[~df.index.duplicated(keep='first')]

Better Workflow

Zerve shows dataframe index and shape inline, making it easier to spot when an index has gotten messy after merges or filters.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes