看起来您的问题是想要按索引合并,但是您将它们视为列了?尝试稍微修改一下合并代码 -
player_runs.merge(matches_played_by_players,
left_index=True,
right_index=True,
how='left')
此外,请确保两个索引的类型相同(在这种情况下,考虑使用int
而不是)。str
player_runs.index = player_runs.index.astype(int)
And,
matches_played_by_players.index = matches_played_by_players.index.astype(int)
你基本上是在不存在的列上进行合并。这是因为 reset_index
创建了一个新的数据框,而不是改变应用它的数据框。当使用 reset_index 时设置参数 inplace=True
应该可以解决这个问题,或者在每个数据框的索引上进行合并。例如:
pd.merge(df1,df2,left_index=True,right_index=True,how='left')
Int64
类型,我运行了你的代码,出现了一个新的错误:ValueError: left_index parameter must be of type bool, not <class 'str'>
。 - Jaskaran Singh PuriTrue
,而不是"True"
:-) - cs95