python - write numpy array to CSV with row indices and header -


i have numpy array.

values = np.array([14, 21, 13, 56, 12]) 

i want write values in 1 column of csv file, row indices in column, , header. found function:

numpy.savetxt("foo.csv", values, header="id,values", delimiter=",") 

i'm not sure how add indices (1, 2, 3, 4, 5). also, header turns out # id,values. i'm not sure # came from. get:

# id,values 14 21 13 56 12 

i want this:

id,values 1,14 2,21 3,13 4,56 5,12 

there may better way don't think can directly numpy.savetxt:

import  numpy np  arr = np.array([14 ,21, 13, 56, 12]) np.savetxt("foo.csv", np.dstack((np.arange(1, arr.size+1),arr))[0],"%d,%d",header="id,values") 

*the # default behaviour documented:

string prepended header , footer strings, mark them comments. default: ‘# ‘, expected e.g. numpy.loadtxt.*

you use comments="" there reason.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -