python—迭代df中的行并基于这些值创建新列

mec1mxoz  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(382)

我想创建一个新的相对分数(列),将f1车手与给定年份和给定车队内的队友进行比较。
我的数据如下所示:

stats_df.head()

>       driver  year    team    points
>     0 AIT 2020    Williams    0.0
>     1 ALB 2019    Red Bull    76.0
>     2 ALB 2019    AlphaTauri  16.0
>     3 ALB 2020    Red Bull    105.0
>     4 ALO 2013    Ferrari     242.0

我累了:

teams = stats_df['team'].unique()
years = stats_df['year'].unique()
drivers = stats_df['driver'].unique()

for year in years:
    for team in teams:
        team_points = stats_df['points'].loc[stats_df['team']==team].loc[stats_df['year']==year].sum()
        for driver in drivers:
            driver_points = stats_df['points'].loc[stats_df['team']==team].loc[stats_df['year']==year].loc[stats_df['driver']==driver]
            power_score = driver_points/(team_points/2)
            stats_df['power_score'].loc[stats_df['team']==team].loc[stats_df['year']==year].loc[stats_df['driver']==driver] = power_score

导致新列中出现NaN('power_score')。
我们将不胜感激。

eoxn13cs

eoxn13cs1#

查看您的代码,您可以计算 team_points 利用 .groupby(["team", "year"]) 然后简单地分开 points 使用这些值:

team_points = df.groupby(["team", "year"])["points"].transform("sum")
df["power_score"] = df["points"] / (team_points / 2)
print(df)

印刷品:

driver  year        team  points  power_score
0    AIT  2020    Williams     0.0          NaN
1    ALB  2019    Red Bull    76.0          2.0
2    ALB  2019  AlphaTauri    16.0          2.0
3    ALB  2020    Red Bull   105.0          2.0
4    ALO  2013     Ferrari   242.0          2.0

相关问题