比较Python数据帧中的一行和下一行

比较Python数据帧中的一行和下一行,python,parsing,compare,dataframe,Python,Parsing,Compare,Dataframe,我想将python数据帧中的一行与其下一行进行比较,如果它们相同,则进行一些添加。但是,我写的代码不起作用 我的播放数据帧头在下面 GameCode PlayNumber PeriodNumber Clock OffenseTeamCode \ 0 299004720130829 1 1 900 47 1 299004720130829 2

我想将python数据帧中的一行与其下一行进行比较,如果它们相同,则进行一些添加。但是,我写的代码不起作用

我的播放数据帧头在下面

          GameCode  PlayNumber  PeriodNumber  Clock  OffenseTeamCode  \
0  299004720130829           1             1    900               47   
1  299004720130829           2             1    NaN              299   
2  299004720130829           3             1    NaN              299   
3  299004720130829           4             1    NaN              299   
4  299004720130829           5             1    NaN              299   

   DefenseTeamCode  OffensePoints  DefensePoints  Down  Distance  Spot  \
0              299              0              0   NaN       NaN    65   
1               47              0              0     1        10    75   
2               47              0              0     2         7    72   
3               47              0              0     3         1    66   
4               47              0              0     1        10    64   

  PlayType  DriveNumber  DrivePlay  
0  KICKOFF          NaN        NaN  
1     RUSH            1          1  
2     PASS            1          1  
3     RUSH            1          1  
4     RUSH            1          1  
我想比较第一行的游戏代码,它与第二行匹配,做一些添加它们的操作,等等。但是我在下面的代码中得到了一个错误

print play.head()
df = pd.DataFrame()

rushingyards = 0
passingyards = 0

for row in play.itertuples():
    if df.empty:
        df = play
    else:
        if play['GameCode'] == df['GameCode']:
            if play['PlayType'] in ('RUSH','PASS'):
                if play['PlayType']=='RUSH':
                    rushingyards = rushingyards+play['Distance']
                else:
                    passingyards  = passingyards + play['Distance'] 

请提供帮助。

也许您正在寻找groupby/sum操作:

yards = df.groupby(['GameCode', 'PlayType'])['Distance'].sum().unstack('PlayType')
# PlayType         KICKOFF  PASS  RUSH
# GameCode                            
# 299004720130829      NaN     7    21
对于每个
GameCode
PlayType
,这将计算
距离的总和。
unstack
返回一个数据帧,索引为
GameCode
s,列为
PlayType
s



注意
passing_yards
rushing_yards
将是一个系列,索引为
GameCodes

我发现一个问题是您没有指定行。尝试df.ix或df.iloc在比较中使用该行,看看这是否解决了您的问题。Ix可能特别有用,因为您只需要一个行号和一个列名。这正是我想要的。有没有一种方法可以将传球和冲刺码与单个游戏代码结合起来?比如,游戏代码,通行码,冲刺码都在一张桌子上。没有重复的游戏代码?你能帮我解决上述问题吗@你能发布你想要的结果吗?我不确定您是否要将传球码和冲刺码相加:
yards['PASS']+yards['RUSH']
或者您是否要删除
KICKOFF
列:
yards.drop('KICKOFF',axis=1)
,或者其他什么……或者您可能要将特定游戏码的传球码和冲刺码相加,如
yards.loc[299004720130829,‘PASS']+yards.loc[299004720130829,‘RUSH']
。但请注意,您通常不希望处理单个游戏代码——您可以从熊猫中获得更好的性能(以及更优雅的代码)如果您可以使用Pandas方法一次计算所有游戏代码所需的内容。我希望得到如下结果。游戏代码:299004720130829 893 401 299004720130824 450 657299004720130821435357我想要一个这样的表,这样我就可以用它来分析了。
import numpy as np
import pandas as pd
nan = np.nan

df = pd.DataFrame(
    {'Clock': [900.0, nan, nan, nan, nan],
     'DefensePoints': [0, 0, 0, 0, 0],
     'DefenseTeamCode': [299, 47, 47, 47, 47],
     'Distance': [nan, 10.0, 7.0, 1.0, 10.0],
     'Down': [nan, 1.0, 2.0, 3.0, 1.0],
     'DriveNumber': [nan, 1.0, 1.0, 1.0, 1.0],
     'DrivePlay': [nan, 1.0, 1.0, 1.0, 1.0],
     'GameCode': [299004720130829, 299004720130829, 299004720130829,
                  299004720130829, 299004720130829],
     'OffensePoints': [0, 0, 0, 0, 0],
     'OffenseTeamCode': [47, 299, 299, 299, 299],
     'PeriodNumber': [1, 1, 1, 1, 1],
     'PlayNumber': [1, 2, 3, 4, 5],
     'PlayType': ['KICKOFF', 'RUSH', 'PASS', 'RUSH', 'RUSH'],
     'Spot': [65, 75, 72, 66, 64]})

yards = df.groupby(['GameCode', 'PlayType'])['Distance'].sum().unstack('PlayType')
passing_yards, rushing_yards = yards['PASS'], yards['RUSH']