Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a large-ish numpy array containing floats, which I save as a csv file with np.savetxt("myFile.csv", myArray, delimiter=",")

Now, as the array has many columns and it can become hard to remember what is what, I want to add a string header to the array before exporting it. Since numpy doesn't accept strings in float arrays, is there a trick to accomplish this?

[Solution] Thanks to Cyborg's advice, I managed to make this work installing Pandas.

import pandas as pd
df = pd.DataFrame(A) # A is a numpy 2d array
df.to_excel("A.xls", header=C,index=False) # C is a list of string corresponding to the title of each column of A
share|improve this question

1 Answer 1

up vote 3 down vote accepted

The header argument (the docs):

numpy.savetxt(fname, X, delimiter=' ', header='')

But you may prefer Pandas if you are actually dealing with a table.

share|improve this answer
    
I am indeed dealing with a 2d array, this is why I believed the 'header' attribute wouldn't work. I'll try panda! Cheers! –  Rodolphe Dec 1 '13 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.