在数据帧之间映射值

2024-09-30 01:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个{}:

   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)

我有这个{}:

   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)

现在我尝试使用两个dfs上的“player_id”将rank值从df_players传递到df_games,如下所示:

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

但所有级别都在操作后打印NaN


我错过了什么


Tags: columnsiddfprnullnumgamesplayer
2条回答

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

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)

您可以使用pd.merge从df_游戏中获得排名

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

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

相关问题 更多 >

    热门问题