pandas concat 错误

ValueError: Shape of passed values is (r1, c1), indices imply (r1, c2)

关于pandas合并dataframe错误

问题描述

执行下面代码时引起 ValueError: Shape of passed values is (r1, c1), indices imply (r1, c2) 错误(注:r1、c1、c2为整数,c1 != c2):

pd.concat([df1, df2], axis=1, join='outer')

错误原因

df1或df2中存在重复的index,导致合并时发生逻辑冲突

解决方法

df1 = df1.loc[df1.index.drop_duplicates(keep=False),:] # 去掉df1中重复索引
df2 = df2.loc[df2.index.drop_duplicates(keep=False),:] # 去掉df2中重复索引
pd.concat([df1, df2], axis=1, join='outer')
发布了40 篇原创文章 · 获赞 4 · 访问量 4279

猜你喜欢

转载自blog.csdn.net/qq_38607066/article/details/103958279
今日推荐