I want to save a 2D array to a CSV file with row and column "header" information (like a table). I know that I could use the header argument to numpy.savetxt to save the column names, but is there any easy way to also include some other array (or list) as the first column of data (like row titles)?
Below is an example of how I currently do it. Is there a better way to include those row titles, perhaps some trick with savetxt I'm unaware of?
import csv
import numpy as np
data = np.arange(12).reshape(3,4)
# Add a '' for the first column because the row titles go there...
cols = ['', 'col1', 'col2', 'col3', 'col4']
rows = ['row1', 'row2', 'row3']
with open('test.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(cols)
for row_title, data_row in zip(rows, data):
writer.writerow([row_title] + data_row.tolist())