我运行这段代码
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)但是,是否有此错误:仅对唯一值的Index对象进行重新索引有效,是否有人知道解决方案?谢谢
发布于 2022-02-23 20:04:48
运行pd.concat时,每个源DataFrame必须具有唯一的索引。
首先确定哪个源DataFrame有一个非唯一的索引.对于每个源DataFrame (假设它是df),运行:
df.index.is_unique(这是一个属性,而不是一个方法,因此不要使用括号)。
错误的结果意味着这个DataFrame中的索引是非唯一的。然后删除具有重复索引值的行:
df = df.loc[~df.index.duplicated(keep='first')]为了不丢失原始数据,也许您应该将结果保存在一个新的临时DataFrame下,然后使用它来连接这些临时DataFrames。
https://stackoverflow.com/questions/71240439
复制相似问题