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