【发布时间】:2016-02-11 21:16:14
【问题描述】:
我想在 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']
请帮忙。
【问题讨论】:
-
我看到可能存在问题的一件事是您没有指定行。尝试 df.ix 或 df.iloc 在您的比较中使用该行,看看是否能解决您的问题。 Ix 可能特别有用,因为您只需要一个行号和一个列名。
标签: python parsing compare dataframe