熊猫-数据框之间的映射ID

时间:2020-08-10 14:28:24

标签: python pandas

我需要映射两个数据框。

df_players

name            711 non-null object
player_id       711 non-null int64
round_id       711 non-null int64
team_id         711 non-null int64

df_games

game_id         10 non-null int64
team_home_id    10 non-null int64
team_away_id    10 non-null int64

现在我需要将信息从df_games带到我的df_players,如果团队参加比赛,则将值增加1;如果团队参加比赛,则将值增加0。


我正在尝试在df_players上添加一列首页,然后单独添加一列,

df_players['home'] = df_players['team_id'].map(df_games['team_home_id'])
df_players['away'] = df_players['team_id'].map(df_games['team_away_id'])

但是我得到了所有NaN


我如何达到我想要的结果?

1 个答案:

答案 0 :(得分:0)

这不是地图,请检查isin

df_players['home'] = df_players['team_id'].isin(df_games['team_home_id'])
df_players['away'] = df_players['team_id'].isin(df_games['team_away_id'])