我一直在尝试操作一些代码来查看第 3 列中的不同uniqueclassindicator
,如果第 3 列是2
然后查看同一行中的前一行uniqueclassindicator
以从该行检索数据。此外,我的输出只有在第 3 列和第 6 列都是1
.
我一直在使用的代码:
from collections import defaultdict
import csv
# you probably can think up better names
fields = ('TitleA', 'TitleB', 'TitleIndicator', 'TitleRNum', 'TitleC', 'TitleD', 'TitlePNum', 'TitleBF', 'TitleCheck')
entries = defaultdict(dict)
with open("exampledata.csv", 'rb') as fd:
reader = csv.DictReader(fd, fields)
for counter, row in enumerate(reader):
if counter != 0:
TitleRNum = int(row['TitleRNum'])
if row['TitlePNum']:
TitlePNum = int(row['TitlePNum'])
else:
TitlePNum = ""
Check = row['TitleCheck']
Name = row['TitleB']
key = (TitleRNum, TitleRNum)
previous = entries[row['TitleIndicator']]
if Check:
# Scenario 1
if (1, 1) in previous:
if (key[0] == 2 and key[1]>=2) or key[1] is None: # If Rank 2 and Position is Anything
if TitleRNum == 2:
p = previous[(2, 1)]
print '{p[TitleB]} {r[TitleB]} {p[TitleRNum]} {r[TitleRNum]} {p[TitlePNum]} {r[TitlePNum]} {p[TitleBF]} {r[TitleBF]} {p[TitleCheck]} {r[TitleCheck]}'.format(p=p, r=row)
# remember this row for later rows to match against.
previous[key] = row
示例数据:
TitleA,TitleB,TitleIndicator,TitleRNum,TitleC,TitleD,TitlePNum,TitleBF,TitleCheck
DataA,Joe,uniqueclassindicator1,1,125,3.659819202,1,984.2,Yes
DataA,Bob,uniqueclassindicator1,2,125,4.212490883,2,994.2,Yes
DataA,Dilon,uniqueclassindicator1,3,125,10.4587985,3,1023.2,Yes
DataA,Jessie,uniqueclassindicator1,4,125,12.68794408,9,,
DataA,Sammy,uniqueclassindicator1,5,125,13.91781792,9,,
DataA,Tommy,uniqueclassindicator1,6,125,15.68662715,9,,
DataA,Bobby,uniqueclassindicator1,7,125,16.45724048,9,,
DataA,Liam,uniqueclassindicator1,8,125,17.80601624,9,,
DataA,George,uniqueclassindicator1,9,125,29.77286312,9,,
DataA,Aaron,uniqueclassindicator1,10,125,34.68115514,9,,
DataA,Tim,uniqueclassindicator2,1,125,4.495485874,3,992.13,E
DataA,Tom,uniqueclassindicator2,2,125,4.899056741,2,951.1,E
DataA,Ryan,uniqueclassindicator2,3,125,6.856404461,1,932.1,E
DataA,Jack,uniqueclassindicator2,4,125,8.773751853,9,,
DataA,Jennifer,uniqueclassindicator2,5,125,9.16233388,9,,
DataA,Sarah,uniqueclassindicator2,6,125,10.16514897,9,,
DataA,Joanne,uniqueclassindicator2,7,125,18.43432845,9,,
DataA,Jess,uniqueclassindicator2,8,125,19.28867787,9,,
DataA,Test1,uniqueclassindicator3,1,125,4.634033328,9,,
DataA,Test2,uniqueclassindicator3,2,125,6.470141577,9,,
DataA,Test3,uniqueclassindicator3,3,125,7.337664044,3,33.59,Yes
DataA,Test4,uniqueclassindicator3,4,125,7.614649866,1,30,D
DataA,Test5,uniqueclassindicator3,5,125,10.02454642,9,,
DataA,Test6,uniqueclassindicator3,6,125,11.95552344,9,,
DataA,Test7,uniqueclassindicator3,7,125,14.36021059,2,33.58,E
DataA,Test8,uniqueclassindicator3,8,125,14.73472778,9,,
DataA,Test9,uniqueclassindicator3,9,125,24.30844993,9,,
DataA,Mark,uniqueclassindicator4,1,125,4.634033328,1,395.1,F
DataA,Jason,uniqueclassindicator4,2,125,6.470141577,9,,
DataA,Dan,uniqueclassindicator4,3,125,7.337664044,9,,
DataA,Tessa,uniqueclassindicator4,4,125,7.614649866,2,395.2,F
DataA,Laura,uniqueclassindicator4,5,125,10.02454642,9,,
DataA,Lizz,uniqueclassindicator4,6,125,11.95552344,9,,
DataA,Hannah,uniqueclassindicator4,7,125,14.36021059,3,395.3,F
DataA,Ian,uniqueclassindicator4,8,125,14.73472778,9,,
DataA,Janet,uniqueclassindicator4,9,125,24.30844993,9,,
期望的输出:
Joe,Bob,1,2,1,2,984.2,994.2,Yes,Yes
Mark,Jason,1,2,1,9,295.1,,F,,
因此,为了澄清 TitleIndicator/Uniqueclassindicator 定义的每个组,如果第 6 列和第 3 列都等于 1,我希望能够从该组的前两行中提取数据。
如果有人能告诉我如何修复此代码,将不胜感激。非常感谢