假设我有这两个表:
表格1:
ID CODE DATE value1 value2 text
-----------------------------------------------------
1 13A 2012-05-04 12.0 0.0 null
2 13B 2011-06-08 5.5 0.0 null
3 13C 2012-07-05 4.0 0.0 null
4 13D 2010-09-09 7.7 0.0 null
1 13A .....................................
1 13D .....................................
3 13D .....................................
表2:
CODE DESCRIPTION
------------------
13A DISEASE1
13B DISEASE2
13C DISEASE3
13D DISEASE4
我想找到一种有效的方法来计算每个 id 的代码出现次数,并根据第二个表中的代码创建计数向量。例如:
[2,0,0,1] 表示 id=1 的人的计数向量,其中每个值是表 2 中代码的出现次数
我设法做到了这一点,但看起来效率不高......有没有更有效的方法?
sql = "SELECT * FROM table1"
cursor.execute(sql)
table1 = cursor.fetchall()
sql2 = "SELECT CODE FROM table2"
cursor.execute(sql2)
codes = cursor.fetchall()
list1 = []
list2 = []
cnt = Counter()
countList = []
n=len(codes)
for id,iter in itertools.groupby(table1,operator.itemgetter('ID')):
idList = list(iter)
list1.append(list((z['CODE']) for z in idList))
for pat in list1:
for code in codes:
cnt=pat.count(code.get('CODE'))
list2.append(cnt)
countList = [list2[i:i+n] for i in range(0, len(list2), n)]