我正在处理大量的csv数据,我想把不同位置的几列放入一列中,用分号(;)分隔。在
所以我现在的情况是。。在a b c d
1 2 3 4
1 2 3 4
1 2 3 4
我想把它改成…这个,所以我所有的数据都只在d列
^{pr2}$
我知道如何删除这些空列a、b和c,但我无法找到将a、b、c列中的数据合并到d列中的方法。
提前谢谢。在
到目前为止我得到的代码是。。在# Parsing the custom formatted data with csv module.
# reads the custom format input and spits out the output in VCF format.
import csv
# input and output
with open('1-0002', 'rb') as csvin, open('converted1','wb') as csvout:
# reading and writing are all tab delimited
reader = csv.reader(csvin, delimiter = '\t')
writer = csv.writer(csvout, delimiter = '\t')
# add headings before the for loop to prevent the heading being affected by column manipulation.
writer.writerow(["#CHROM","POS","ID","REF","ALT","QUAL","FILTER","INFO"])
for row in reader:
# deleting unnecessary columns, 'del' operator must be in ascending order or else it will give range error
# manually deleting columns since the input data is in custom format.
del row[11]
del row[10]
del row[9]
del row[8]
del row[7]
del row[6]
del row[5]
del row[1]
del row[0]
# inserting 1 and . in specific columns
row.insert(0,'1')
row.insert(2,'.')
row.insert(5,'.')
row.insert(7,'') # inserting empty column for INFO headings.
# change 'YES' to 'PASS' , leaving HETERO as it is.
if row[6] == 'YES':
row[6] = 'PASS'
writer.writerow(row)
所以从上面的代码中,我想把来自不同列的数据放入INFO列中。在