Search code examples
python-3.xnumpyexport-to-csv

Missing column in CSV file


So I've a 2D-array looking like this:

[[1 0]
[2 0]
[3 0]
[4 0]
...

and I want to save it to a csvfile, I know that I must use to_csv to do so.

So I tried doing : np.savetxt("file.csv",array,delimiter=',',fmt='%d,%d'), %d is to store data as int not as the default format But my csv file only contains the first column and not the column of zero.


Solution

  • Are you sure array is what you claim? If I make it fresh:

    In [234]: arr = np.array([[1, 0],
         ...: [2, 0],
         ...: [3, 0],
         ...: [4, 0]])
    
    In [235]: arr
    Out[235]: 
    array([[1, 0],
           [2, 0],
           [3, 0],
           [4, 0]])
    

    Your savetxt works fine (the delimiter parameter isn't needed since you included it in the fmt):

    In [237]: np.savetxt("file.csv",arr,delimiter=',',fmt='%d,%d')
    
    In [238]: !more file.csv
    1,0
    2,0
    3,0
    4,0