给数据帧添加一个列,该列是来自另一个数据帧的条件求和。

2024-04-28 12:33:06 发布

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

我有两个数据框,一个是棒球队的数据,另一个是球员信息。我需要在team data框架中添加一个team salary列,该列按年度和团队查找薪资数据,并返回该年度/团队的球员薪资总和。我尝试过许多不同的方法,但我认为我最接近这个方法:

def get_team_salary(year, team):
    data_slice = salary_data_df[(salary_data_df.yearID == year) & 
                                (salary_data_df.teamID == team)]
    return data_slice['salary'].sum()

#This line of code works correctly without the next function in the code.
#team_data_df['team_salary'] = get_team_salary(2000,'ANA')

def assign_team_salaries(team_data_df):
    year = team_data_df['yearID']
    team = team_data_df['teamID']
    return team_data_df.applymap(get_team_salary(year, team))

team_data_df['team_salary'] = assign_team_salaries(team_data_df)

assign_team_salaries函数调用不起作用。我尝试了很多不同的方法来修复它,并且收到了很多不同的错误消息。你得到的是"ValueError: Can only compare identically-labeled Series objects"

有人能帮我找出我做错了什么吗?我尝试过完全不同的方法,比如对薪资数据使用groupby,然后先合并两个数据帧,但我也没能让它们起作用。蒂亚!你知道吗

team_data_df有大量列,但相关列(按顺序)如下所示:

teamID    yearID
2000      ANA
2000      ARI
...       ...
2016      TOR
2016      WSN

salary_data_df有相关列:

teamID   yearID   playerID   salary
2000     ANA      anderga01  3250000
...      ...      ...        ...
2016     WSN      zimmery01  14000000

Tags: 数据方法dfdataget薪资yearteam
1条回答
网友
1楼 · 发布于 2024-04-28 12:33:06

如您所述,您可以在salary_data_df上使用.groupby,然后将这些和合并到team_data_df。你知道吗

举以下两个小例子:

print(team_data_df)
  teamID  yearID
0      a    2000
1      b    2000
2      c    2000
3      a    2001
4      b    2001
5      c    2001

print(salary_data_df)
   teamID  yearID  playerID  salary
0       a    2000         1     100
1       a    2000         2     200
2       b    2000         4     300
3       b    2000         5     400
4       b    2000         6     500
5       c    2000         7     600
6       a    2001         1     700
7       a    2001         2     800
8       a    2001         3     900
9       b    2001         4    1000
10      b    2001         5    1100
11      c    2001         7    1200
12      c    2001         8    1300

然后:

sums = (salary_data_df
        .groupby(by=['yearID', 'teamID'])
        .sum()['salary']
        .reset_index())
    # alternative: use parameter `as_index=True` instead of `.reset_index()`

res = team_data_df.merge(sums, on=['yearID', 'teamID'])

print(res)
  teamID  yearID  salary
0      a    2000     300
1      b    2000    1200
2      c    2000     600
3      a    2001    2400
4      b    2001    2100
5      c    2001    2500

您可能还需要注意merge的on参数。它们模仿类似SQL的合并规范。你知道吗

相关问题 更多 >