python data_error ai_generated true

ValueError: cannot reindex on an axis with duplicate labels

ID: python/pandas-cannot-reindex-duplicate

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 90% success
    df = df.loc[~df.index.duplicated(keep='first')]; df.reindex(new_index)
  2. 85% success
    Aggregate first: df = df.groupby(level=0).sum(); df.reindex(new_index)

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Same error, duplicates still present

  2. 85% fail

    Sorting doesn't dedupe