Python数据框未在索引上合并 [英] Python Dataframes not merging on index
本文介绍了Python数据框未在索引上合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试合并2个数据帧,但是由于某种原因,它会抛出KeyError: Player_Id
I'm trying to merge 2 dataframes, but for some reason it's throwing KeyError: Player_Id
我正在尝试在Striker_Id
和Player_Id
这是我的数据框的外观
合并代码:
player_runs.merge(matches_played_by_players,left_on='Striker_Id',right_on='Player_Id',how='left')
我在做什么错了?
推荐答案
嗯,从您的问题来看,似乎您正在尝试合并索引,但是您将它们视为列?尝试稍微更改合并代码-
Hmm, from looking at your problem, it seems like you're trying to merge on the indexes, but you treat them as columns? Try changing your merge code a bit -
player_runs.merge(matches_played_by_players,
left_index=True,
right_index=True,
how='left')
此外,请确保两个索引均为相同类型(在这种情况下,请考虑 str
int
?)
Furthermore, make sure that both indexes are of the same type (in this case, consider str
int
?)
player_runs.index = player_runs.index.astype(int)
然后
matches_played_by_players.index = matches_played_by_players.index.astype(int)
这篇关于Python数据框未在索引上合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文