I have been trying to solve a basic problem (and have been Python 2.7, and have got no where. I have a csv file with column headings such as:
a,b,c
1,2,3
1,2,3
1,2,3
This is saved as test1.csv
I have managed to take each column and pass them into an array, so each column heading is at the start, followed by the data. How would I then write this back into a new csv file with the same order and structure?
import csv
f = open('test1.csv','rb')
reader = csv.reader(f)
headers = reader.next()
print headers
column = {}
for h in headers:
column[h] = []
for row in reader:
for h,v in zip(headers,row):
column[h].append(v)
pandasis out of the question?zip(*d.values())(gives you a list for each line)zip(*map(lambda x: x[1], sorted(d.items(), key=lambda x: header.index(x[0]))))(but I prefer the SortedDict)