所以,我正在使用一个脚本来计算一个人在行中指定日期之前的日期出现在列表中的次数,并且在第 6 列中出现 1,并且还计算了一个人的次数(列7) 出现在行中指定日期之前的日期列表中(注意它们按时间顺序排序。)(使用基于零的列引用)
示例数据集
02/01/2005,Data,Class xpv,4,11yo+,4,1,George Smith
02/01/2005,Data,Class xpv,4,11yo+,4,2,Ted James
02/01/2005,Data,Class xpv,4,11yo+,4,3,Emma Lilly
02/01/2005,Data,Class xpv,4,11yo+,4,5,George Smith
02/01/2005,Data,Class xpv,4,11yo+,6,4,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,2,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,5,George Smith
03/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips
03/01/2005,Data,Class tn2,4,10yo+,6,1,Emma Lilly
03/01/2005,Data,Class tn2,4,10yo+,6,6,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,6,Ted James
04/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips
04/01/2005,Data,Class tn2,4,10yo+,6,2,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,4,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,1,George Smith
04/01/2005,Data,Class tn2,4,10yo+,6,5,Tom Phillips
05/01/2005,Data,Class 22zn,2,10yo+,5,3,Emma Lilly
05/01/2005,Data,Class 22zn,2,10yo+,5,1,Ted James
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips
我正在使用的代码
import csv
import datetime
import copy
from collections import defaultdict
with open(r"C:\Temp\test.csv") as i, open(r"C:\Temp\resuls.csv", "wb") as o:
rdr = csv.reader(i)
wrt = csv.writer(o)
data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None
for line in rdr:
date, name = datetime.datetime.strptime(line[0], '%d/%m/%Y'), line[7]
if date != currdate or not currdate:
for v in data.itervalues(): v[:2] = v[2:]
currdate = date
wrt.writerow(line + data[name][:2])
data[name][3] += 1
if line[6] == "1": data[name][2] += 1
这将返回:
02/01/2005,Data,Class xpv,4,11yo+,4,1,George Smith,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,2,Ted James,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,3,Emma Lilly,0,0
02/01/2005,Data,Class xpv,4,11yo+,4,5,George Smith,0,0
02/01/2005,Data,Class xpv,4,11yo+,6,4,Tom Phillips,0,0
03/01/2005,Data,Class tn2,4,10yo+,6,2,Tom Phillips,0,1
03/01/2005,Data,Class tn2,4,10yo+,6,5,George Smith,1,2
03/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips,0,1
03/01/2005,Data,Class tn2,4,10yo+,6,1,Emma Lilly,0,1
03/01/2005,Data,Class tn2,4,10yo+,6,6,George Smith,1,2
04/01/2005,Data,Class tn2,4,10yo+,6,6,Ted James,0,1
04/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips,0,3
04/01/2005,Data,Class tn2,4,10yo+,6,2,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,4,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,1,George Smith,1,4
04/01/2005,Data,Class tn2,4,10yo+,6,5,Tom Phillips,0,3
05/01/2005,Data,Class 22zn,2,10yo+,5,3,Emma Lilly,1,2
05/01/2005,Data,Class 22zn,2,10yo+,5,1,Ted James,0,2
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith,2,7
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly,1,2
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips,0,5
最终我会想要对我生成的百分比数据执行卡方。但是,现在我想要实现的只是能够计算和求和唯一数据类(第 2 列)中任何一个人的分数机会,并将其作为新列附加到 csv 中。我不确定是否可以编辑我使用的代码以实现这一点作为一个多合一的代码。任何关于如何最好地做到这一点的建设性建议或意见将不胜感激。
我想要的输出如下:
02/01/2005,Data,Class xpv,4,11yo+,5,1,George Smith,0,0,0
02/01/2005,Data,Class xpv,4,11yo+,5,2,Ted James,0,0,0
02/01/2005,Data,Class xpv,4,11yo+,5,3,Emma Lilly,0,0,0
02/01/2005,Data,Class xpv,4,11yo+,5,5,George Smith,0,0,0
02/01/2005,Data,Class xpv,4,11yo+,5,4,Tom Phillips,0,0,0
03/01/2005,Data,Class tn2,4,10yo+,5,2,Tom Phillips,0,1,0.2, He gets 0.2 because there was a 1 in 5 chance for previous occurrences on dates prior to today. 1/5
03/01/2005,Data,Class tn2,4,10yo+,5,5,George Smith,1,2,0.4, He gets 0.4 because there was a 2 in 5 chance for previous occurrences on dates prior to today. 2/5
03/01/2005,Data,Class tn2,4,10yo+,5,3,Tom Phillips,0,1,0.2
03/01/2005,Data,Class tn2,4,10yo+,5,1,Emma Lilly,0,1,0.2
03/01/2005,Data,Class tn2,4,10yo+,5,6,George Smith,1,2,0.4
04/01/2005,Data,Class tn2,4,10yo+,6,6,Ted James,0,1,0.2
04/01/2005,Data,Class tn2,4,10yo+,6,3,Tom Phillips,0,3,0.6
04/01/2005,Data,Class tn2,4,10yo+,6,2,George Smith,1,4,0.8
04/01/2005,Data,Class tn2,4,10yo+,6,4,George Smith,1,4,0.8
04/01/2005,Data,Class tn2,4,10yo+,6,1,George Smith,1,4,0.8
04/01/2005,Data,Class tn2,4,10yo+,6,5,Tom Phillips,0,3,0.4
05/01/2005,Data,Class 22zn,2,10yo+,5,3,Emma Lilly,1,2,0.4
05/01/2005,Data,Class 22zn,2,10yo+,5,1,Ted James,0,2,0.366666667
05/01/2005,Data,Class 22zn,2,10yo+,5,2,George Smith,2,7,1.3
05/01/2005,Data,Class 22zn,2,10yo+,5,4,Emma Lilly,1,2,0.4
05/01/2005,Data,Class 22zn,2,10yo+,5,5,Tom Phillips,0,5,0.733333333