[英]Python Dataframes not merging on index
嗯,从您的问题来看,似乎您正在尝试合并索引,但是您将它们视为列? 尝试稍微更改合并代码-
player_runs.merge(matches_played_by_players,
left_index=True,
right_index=True,
how='left')
此外,请确保两个索引属于同一类型 (在这种情况下,请考虑使用str
int
?)
player_runs.index = player_runs.index.astype(int)
和,
matches_played_by_players.index = matches_played_by_players.index.astype(int)
您基本上是在不存在的列上进行合并。 这是因为reset_index
会创建一个新的数据帧,而不是更改其应用于的数据帧。 在使用reset_index时设置参数inplace inplace=True
应该可以解决此问题,或者在每个数据帧的索引上合并。 即
pd.merge(df1,df2,left_index=True,right_index=True,how='left')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.