简体   繁体   中英

Reshaping CSV columns in python

I have my data in this form

在此处输入图像描述

and the required form of data is

在此处输入图像描述

Can anybody help me in this regard?


The content of the initial CSV file as text is:

var1,var2,col1,col2,col3
a,f,1,2,3
b,g,4,5,6
c,h,7,8,9
d,i,10,11,12

You can do it directly with the csv module. You just read from the initial file, and write up to 3 rows per initial row into the resulting file:

with open('in.csv') as fdin, open('out.csv', 'w', newline='') as fdout:
    rd = csv.reader(fdin)
    wr = csv.writer(fdout)
    header = next(rd)       # read and process header
    _ = wr.writerow(header[:2] + ['columns',''])
    for row in rd:                                  # loop on rows
        for i in range(3):                          # loop on the 3 columns
            try:   
                row2 = row[:2] + ['col{}'.format(i+1), row[2 + i]]
                _ = wr.writerow(row2)
            except IndexError:                      # prevent error on shorter line
                break

If you intend to do heavy data processing, you should contemplate using the Pandas module.

With the data sample, it gives:

var1,var2,columns,
a,f,col1,1
a,f,col2,2
a,f,col3,3
b,g,col1,4
b,g,col2,5
b,g,col3,6
c,h,col1,7
c,h,col2,8
c,h,col3,9
d,i,col1,10
d,i,col2,11
d,i,col3,12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM