I need to iterate the fields and compute sum of few columns group by the value in another column .
For ex base table is
C1 C2 C3 C4 C5
a1 2 3 4 q
a1 4 5 7 a
a2 34 56 6 e
a2 4 5 5 5
a3 3 3 3 4
a3 3 3 3 3
The result table should be
c2 c3 c4
a1 6 8 11
a2 38 61 11
a3 6 6 6
50 75 28
I am able to iterate the fields to get the value of each field but got stuck in creating a two dimension matrix of result format开发者_开发百科. I was looking into 2 dimension array to achieve this scenario.
Working from a lot of assumptions here since the question is quite unspecific...
# table containing only the actual data
table = [[2,3,4,"q"],[4,5,7,"a"],[34,56,6,"e"],[4,5,5,5],[3,3,3,4],[3,3,3,3]]
result = []
# iterate through table[0/2/4/...] zipped together with table[1/3/5/...]
for (row1, row2) in zip(table[::2], table[1::2]):
# add the first three elements of each row and append the results to our result
result.append([c1+c2 for c1,c2 in zip(row1[:3], row2[:3])])
print(result)
outputs
[[6, 8, 11], [38, 61, 11], [6, 6, 6]]
精彩评论