0

I have a csv file that contains five columns:

a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
a3 b3 c3 d3 e3

And I have some lists that I want to add to this csv (each list as a new column), so the end result would look something like this:

a1 b1 c1 d1 e1 f1 g1 h1
a2 b2 c2 d2 e2 f2 g2 h2
a3 b3 c3 d3 e3 f3 g3 h3

I can save these lists as a new csv

outputdata=zip(list1,list2,list3,list4)
writer=CsvUnicodeWriter(open("myoutput.csv","wb"))
writer.writerows(output data)

But this is not exactly what I need. Thanks in advance.

DeffRoll
  • 43
  • 5

1 Answers1

0

The index of the source csv and the zipped lists should match so you can use enumerate to track it.

zipped = zip(list1, list2, list3, list4)

with open('in.csv', 'rb') as infile, open('out.csv' 'wb') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)
    for i, row in enumerate(reader):
        row.extend(zipped[i])
        writer.writerow(row)

If you are doing a lot of data wrangling like this however check out the pandas library. The pandas code is a bit simpler, although you have to get used to the pandas api, which is based on index labels and vectorised operations.

indata = pd.read_csv('in.csv')
zipped = pd.DataFrame(zip(List1, List2, List3, List4)
#axis=1 indicates to concat the frames column wise
outdata = pd.concat([indata, zipped], axis=1)
#we dont want headers and dont want the row labels
outdata.to_csv('out.csv', header=False, index=False)
b10n
  • 1,166
  • 9
  • 8
  • Thank you! That does indeed look like a solution. However, I get a "sequences expected" error, and I'm not quite sure what to do about it. – DeffRoll Sep 15 '14 at 00:17
  • here it is: `108 for i, row in enumerate(reader): 109 outrow = row.extend(zipped[i]) --> 110 writer.writerow(outrow) Error: sequence expected` – DeffRoll Sep 15 '14 at 00:28
  • Oops! Extend returns none as it modifies the object in place. I have updated my answer. Sorry about that, should have tested first. – b10n Sep 15 '14 at 00:36