Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取组的最后一个值,并将该值设置为下一个组_Python_Pandas - Fatal编程技术网

Python 获取组的最后一个值,并将该值设置为下一个组

Python 获取组的最后一个值,并将该值设置为下一个组,python,pandas,Python,Pandas,我的数据集的格式如下所示: >>> df race_year race_id driver_id driver_standings_position 2007 1 1 5 2007 1 2 4 2007 2 1 3 2007 2 2 7 2007 3 1

我的数据集的格式如下所示:

>>> df
    race_year race_id driver_id driver_standings_position
    2007      1       1         5
    2007      1       2         4
    2007      2       1         3
    2007      2       2         7
    2007      3       1         2
    2007      3       2         5
    2008      4       1         1
    2008      4       2         3        
    2008      5       1         2                        
    2008      5       2         1                         
    2008      6       1         3                
    2008      6       2         2                                  
我想要实现的是: 我希望过去几年x车手的最终冠军位置能在整个赛季的新专栏中显示出来。 大概是这样的:

>>> df
    race_year race_id driver_id driver_standings_position championship_position_last_year
    2007      1       1         5
    2007      1       2         4
    2007      2       1         3
    2007      2       2         7
    2007      3       1         2
    2007      3       2         5
    2008      4       1         1                         2
    2008      4       2         3                         5
    2008      5       1         2                         2
    2008      5       2         1                         5
    2008      6       1         3                         2
    2008      6       2         2                         5
我是这样解决的:

test = df[['race_year','race_id','driver_id','driver_standings_position']].copy()
test['race_year'] += +1
test['driver_standings_position_last_year'] = test.groupby(['race_year','driver_id'])\
    ['driver_standings_position'].tail(1)
test

a = test.groupby(['race_year','driver_id'])
a = a.last()
b = a.reset_index()
df= pd.merge(df, b, how="left", left_on=['race_year',"driver_id"], right_on=['race_year',"driver_id"])

# Drop Columns
df= df.drop(columns=['race_id_y', 'driver_standings_position_y'])

# Rename Columns
df= df.rename(columns={"race_id_x": "race_id", "driver_standings_position_x": "driver_standings_position"})

但我感兴趣的是一种更好的/更方便的/pythonic方式

从计算以下辅助数据帧开始:

df2 = df.groupby(['race_year', 'driver_id']).driver_standings_position\
    .last().rename('championship_position_last_year').reset_index()
它在当前的年度中保持每个车手的冠军位置

要将其更改为上一年的位置,请运行:

df2.race_year += 1
要计算最终结果,请运行:

df.merge(df2, how='left', on=['race_year', 'driver_id']).fillna('')
上述解决方案在某种程度上与您的类似,但事实并非如此 更短,更具泛达索风格