8-Bit Borges
8-Bit Borges

Reputation: 10043

Pandas - mapping values between DataFrames

I have this df_players:

   rank  player_id        posicao
0    39      82730        Goleiro
1   136     100651       Atacante
2   140      87863  Meio-Campista
3    66      83257       Atacante
4   139     101290       Atacante

df_players.info():

Data columns (total 3 columns):
rank         733 non-null int64
player_id    733 non-null int64
posicao      733 non-null object
dtypes: int64(2), object(1)

And I have this df_games:

   jogo_id  rodada_id  time_id     time_nome  adversario_id adversario_nome  ...  preco_num  variacao_num  media_num  jogos_num status   ano
0   232423          1    293.0  Athletico-PR            267           Vasco  ...       2.00          0.00        0.0          0   Nulo  2019
1   232423          1    293.0  Athletico-PR            267           Vasco  ...       4.00          0.00        0.0          0   Nulo  2019
2   232423          1    293.0  Athletico-PR            267           Vasco  ...       2.00          0.00        0.0          0   Nulo  2019
3   232423          1    293.0  Athletico-PR            267           Vasco  ...       2.00          0.00        0.0          0   Nulo  2019
4   232423          1    293.0  Athletico-PR            267           Vasco  ...       5.83         -2.17        0.4          1   Nulo  2019

df_games.info():

Data columns (total 19 columns):
...
player_id          30042 non-null int64
...
dtypes: float64(7), int64(7), object(5)

Now I'm trying to pass rank values from df_players to df_games using 'player_id', present on both dfs, like so:

df_games['rank'] = df_games['atleta_id'].map(df_players['rank'])

But ranks are all printing NaN after the operation.


What am I missing?

Upvotes: 1

Views: 63

Answers (2)

Terry
Terry

Reputation: 2811

you are almost there, just add set_index() inside the map

df_games['rank'] = df_games['atleta_id'].map(df_players.set_index('player_id')['rank'])

A more readable way

s = df_players.set_index('player_id')['rank']
df_games['rank'] = df_games['atleta_id'].map(s)

Upvotes: 1

EfehanD
EfehanD

Reputation: 26

You can use pd.merge to bring rank from df_games.

df_games.merge(df_players[['rank','player_id']],on='player_id',how='left')

You can also see more details from documentation of pandas.

Upvotes: 1

Related Questions