python 2.7 - Stack columns based on a content of another column -


i have csv file contain following:

id  flow    testna  4   5   6 item_1    test_1  89  51  67 item_1    test_2  60  67  44 item_1    test_3  111 82  67 item_1  b   test_1  88  78  75 item_1  b   test_2  104 66  66 item_1  b   test_3  109 87  49 item_2    test_1  76  76  88 item_2    test_2  59  44  60 item_2    test_3  91  72  59 item_2  b   test_1  73  50  80 item_2  b   test_2  107 75  67 item_2  b   test_3  119 100 50 

the header is: id flow testna 4 5 6

i want stack last 3 columns, data columns have following results:

id    flow testna label data item_1    test_1  4   89 item_1    test_1  5   51 item_1    test_1  6   67 item_1    test_2  4   60 item_1    test_2  5   67 item_1    test_2  6   44 item_1    test_3  4   111 item_1    test_3  5   82 item_1    test_3  6   67 item_1  b   test_1  4   88 item_1  b   test_1  5   78 item_1  b   test_1  6   75 item_1  b   test_2  4   104 item_1  b   test_2  5   66 item_1  b   test_2  6   66 item_1  b   test_3  4   109 item_1  b   test_3  5   87 item_1  b   test_3  6   49 item_2    test_1  4   76 item_2    test_1  5   76 item_2    test_1  6   88 item_2    test_2  4   59 item_2    test_2  5   44 item_2    test_2  6   60 item_2    test_3  4   91 item_2    test_3  5   72 item_2    test_3  6   59 item_2  b   test_1  4   73 item_2  b   test_1  5   50 item_2  b   test_1  6   80 item_2  b   test_2  4   107 item_2  b   test_2  5   75 item_2  b   test_2  6   67 item_2  b   test_3  4   119 item_2  b   test_3  5   100 item_2  b   test_3  6   50 

any ideas?

a brute-force option be:

with open('data-out.csv', 'w') fo:     open('data.csv', 'r') fi:         header = fi.readline()         cols = [s.strip() s in header.split(',')[-3:]]         print >>fo, 'id,flow,testna,label,data'         line in fi.readlines():             fields = line.strip().split(',')             out_fields = fields[:-3]             in range(3):                 print >>fo, ','.join(out_fields + [cols[i], fields[-3+i]]) 

(of course, if have missing data, etc. won't forgiving).

input data.csv:

id,flow,testna,4,5,6 item_1,a,test_1,89,51,67 item_1,a,test_2,60,67,44 item_1,a,test_3,111,82,67 item_1,b,test_1,88,78,75 item_1,b,test_2,104,66,66 item_1,b,test_3,109,87,49 item_2,a,test_1,76,76,88 item_2,a,test_2,59,44,60 item_2,a,test_3,91,72,59 item_2,b,test_1,73,50,80 item_2,b,test_2,107,75,67 item_2,b,test_3,119,100,50 

output data-out.csv:

id,flow,testna,label,data item_1,a,test_1,4,89 item_1,a,test_1,5,51 item_1,a,test_1,6,67 item_1,a,test_2,4,60 item_1,a,test_2,5,67 item_1,a,test_2,6,44 item_1,a,test_3,4,111 item_1,a,test_3,5,82 item_1,a,test_3,6,67 item_1,b,test_1,4,88 item_1,b,test_1,5,78 item_1,b,test_1,6,75 item_1,b,test_2,4,104 item_1,b,test_2,5,66 item_1,b,test_2,6,66 item_1,b,test_3,4,109 item_1,b,test_3,5,87 item_1,b,test_3,6,49 item_2,a,test_1,4,76 item_2,a,test_1,5,76 item_2,a,test_1,6,88 item_2,a,test_2,4,59 item_2,a,test_2,5,44 item_2,a,test_2,6,60 item_2,a,test_3,4,91 item_2,a,test_3,5,72 item_2,a,test_3,6,59 item_2,b,test_1,4,73 item_2,b,test_1,5,50 item_2,b,test_1,6,80 item_2,b,test_2,4,107 item_2,b,test_2,5,75 item_2,b,test_2,6,67 item_2,b,test_3,4,119 item_2,b,test_3,5,100 item_2,b,test_3,6,50 

Comments