我想创建一个新的相对分数(列),将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')。
我们将不胜感激。
1条答案
按热度按时间eoxn13cs1#
查看您的代码,您可以计算
team_points
利用.groupby(["team", "year"])
然后简单地分开points
使用这些值:印刷品: