ValueError: cannot reindex from a duplicate axis

The error "cannot reindex from a duplicate axis" usually generates when you concatenate, reindexing or resampling a DataFrame which the index has duplicate values . When you get this error, first you have to just check if there is any duplication in your DataFrame column names using the code:

If DataFrame has duplicate index values , then remove the duplicated index:

After you remove the duplicated columns from DataFrame, you should be able to run your DataFrame operations without any error.

how to solve cannot reindex from a duplicate axis


If you don't care about preserving the values of your DataFrame index , and you want them to be unique values, set ignore_index=True.


Alternatively, to overwrite your current DataFrame index with a new one:

or, use .reset_index:

Remove inplace=True if you want it to return the dataframe.


In order to make sure your DataFrame cannot contain duplicate values in the index, you can set allows_duplicate_labels flag to False for preventing the assignment of duplicate values.
 (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About