尝试使用一个数据框中的 id 在另一个不具有相同列名的单独数据框中获取数据

Trying to grab data using an id in one dataframe in another seperate dataframe that do not posses the same column name

我有两个数据框

wizards = {player_id': ["3", "4", "0", "9"],
    'name':["beal", "avdija", "hachimura", "dinwiddie"],
    'total points':}
stat_sheet = {jersey_number': ["9", "9" , "0", "3", "4", "0", "9", "9", , "9" , "0", , "3", 
"4", "0", "9"], 'total points':[40, 20, 12, 14, 55, 67, 10, 22, 22, 5, 3, 12, 5, 1]}
wiz_df = pd.DataFrame(wizards)
weeklystats_df = pd.DataFrame(stat_sheet)

我需要确保将每个玩家的所有分数相加,例如:

Beal 总共得到 17 分,因此他在 wiz_df 中的那一节在 wiz_df

中的排行中得到 17 分

所以这需要为每个玩家完成,因为唯一 ID 是 player_id 和 jersey_number

我尝试编写一个多重嵌套的 for 循环,但它不起作用,我尝试加入也不起作用的表。坚持这一点将不胜感激我能得到的所有帮助。

您可以在 weeklystats_df 上组合 .groupby,然后将 .mergewiz_df 组合:

x = wiz_df.merge(
    weeklystats_df.groupby("jersey_number").sum(),
    left_on="player_id",
    right_index=True,
    how="left",
)
print(x)

打印:

  player_id       name  total points
0         3       beal            17
1         4     avdija            67
2         0  hachimura            89
3         9  dinwiddie           115

wiz_df 使用:

  player_id       name
0         3       beal
1         4     avdija
2         0  hachimura
3         9  dinwiddie

weeklystats_df 使用:

   jersey_number  total points
0              9            40
1              9            20
2              0            12
3              3            14
4              4            55
5              0            67
6              9            10
7              9            22
8              9            22
9              0             5
10             3             3
11             4            12
12             0             5
13             9             1