3

Irun this code

esg_fm_barron = pd.concat([barron_clean.drop(columns = "10 year return", inplace = False),ESG_fixed.drop(columns = 'Name',inplace = False), financial_clean.drop(columns = 'Name',inplace = False)], axis = 'columns', join = 'inner')
esg_fm_barron.rename(columns={'Average (Current)': "Total ESG Score"}, inplace=True)
esg_fm_barron.head(3)

but is get this error : Reindexing only valid with uniquely valued Index objects does anyone know a solution ? thanks

1 Answers1

10

When you run pd.concat, each source DataFrame must have unique index.

First identify which source DataFrame has a non-unique index. For each source DataFrame (assuming it is df) run:

df.index.is_unique

(this is a property, not a method, so put no parentheses).

The false result means that the index in this DataFrame is non-unique. Then drop rows with duplicated index values:

df = df.loc[~df.index.duplicated(keep='first')]

In order not to loose original data, maybe you should save the result under a new temporary DataFrame and then use for concatenation these temporary DataFrames.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • I run this it works :D : ' esg_fm_barron = pd.merge(pd.merge(barron_clean,ESG_fixed,on='Ticker'),financial_clean,on='Ticker') esg_fm_barron.drop(['Name', '10 year return','Name_y'] , axis=1, inplace=True) esg_fm_barron.rename(columns={'Average (Current)': "Total ESG Score"}, inplace=True) esg_fm_barron.rename(columns={'Name_x': "Name"}, inplace=True) esg_fm_barron.head()' – Nader bouchnag Feb 25 '22 at 15:58
  • Just a note, there, is not any method `.is_unique()`. Actually it is an attribute of the index object (maybe a property actually) so the test should be `df.index.is_unique` – Iqigai Jul 14 '22 at 18:38
  • 1
    You're right. I corrected my answer. – Valdi_Bo Jul 15 '22 at 05:59