python
data_error
ai_generated
true
ValueError: cannot reindex on an axis with duplicate labels
ID: python/pandas-cannot-reindex-duplicate
80%Fix Rate
86%Confidence
0Evidence
2024-09-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
df.reindex() called on a DataFrame whose index (or columns) contains duplicate labels. Reindex requires unique labels to map old positions to new ones.
generic中文
对索引(或列)含重复标签的 DataFrame 调用 df.reindex()。重新索引需要唯一标签来映射旧位置到新位置。
Workarounds
-
90% success
df = df.loc[~df.index.duplicated(keep='first')]; df.reindex(new_index)
-
85% success
Aggregate first: df = df.groupby(level=0).sum(); df.reindex(new_index)
Dead Ends
Common approaches that don't work:
-
90% fail
Same error, duplicates still present
-
85% fail
Sorting doesn't dedupe