Pandas – mapping ids between Dataframes

Question:

I have two dataframes I need to map.

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

Now I need to bring home or away info from df_games to my df_players, adding value 1 if team played home, or 0 if team played away.


I am trying add a column home and one away to df_players, like so:

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'])

But I’m getting all NaN.

How do I achieve my desired result?

Asked By: 8-Bit Borges

||

Answers:

This is not map , check with 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'])
Answered By: BENY
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.