熊猫-DataFrames之间的映射值

时间:2020-08-08 17:36:05

标签: python pandas

我有这个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)

我有这个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)

现在,我正尝试使用两个df上都存在的“ player_id”将rank的{​​{1}}值传递给df_players,例如:

df_games

但操作之后,所有等级都将打印df_games['rank'] = df_games['atleta_id'].map(df_players['rank'])


我想念什么?

2 个答案:

答案 0 :(得分:1)

您可以使用pd.merge来提高df_games的排名。

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

您还可以从pandas的文档中查看更多详细信息。

答案 1 :(得分:1)

您快到了,只需在set_index()内添加map

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

更具可读性的方式

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