Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format.
If you want to save your numpy array (e.g. your_array = np.array([[1,2],[3,4]])
) to one cell, you could convert it first with your_array.tolist()
.
Then save it the normal way to one cell, with delimiter=';'
and the cell in the csv-file will look like this [[1, 2], [2, 4]]
Then you could restore your array like this:
your_array = np.array(ast.literal_eval(cell_string))
numpy.savetxt saves an array to a text file.
import numpy
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("foo.csv", a, delimiter=",")
tofile is a convenient function to do this:
import numpy as np
a = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
a.tofile('foo.csv',sep=',',format='%10.5f')
The man page has some useful notes:
This is a convenience function for quick storage of array data. Information on endianness and precision is lost, so this method is not a good choice for files intended to archive data or transport data between machines with different endianness. Some of these problems can be overcome by outputting the data as text files, at the expense of speed and file size.
Note. This function does not produce multi-line csv files, it saves everything to one line.
As already discussed, the best way to dump the array into a CSV file is by using .savetxt(...)
method. However, there are certain things we should know to do it properly.
For example, if you have a numpy array with dtype = np.int32
as
narr = np.array([[1,2],
[3,4],
[5,6]], dtype=np.int32)
and want to save using savetxt
as
np.savetxt('values.csv', narr, delimiter=",")
It will store the data in floating point exponential format as
1.000000000000000000e+00,2.000000000000000000e+00
3.000000000000000000e+00,4.000000000000000000e+00
5.000000000000000000e+00,6.000000000000000000e+00
You will have to change the formatting by using a parameter called fmt
as
np.savetxt('values.csv', narr, fmt="%d", delimiter=",")
to store data in its original format
Also, savetxt
can be used for storing data in .gz
compressed format which might be useful while transferring data over network.
We just need to change the extension of the file as .gz
and numpy will take care of everything automatically
np.savetxt('values.gz', narr, fmt="%d", delimiter=",")
Hope it helps
In Python we use csv.writer() module to write data into csv files. This module is similar to the csv.reader() module.
import csv
person = [['SN', 'Person', 'DOB'],
['1', 'John', '18/1/1997'],
['2', 'Marie','19/2/1998'],
['3', 'Simon','20/3/1999'],
['4', 'Erik', '21/4/2000'],
['5', 'Ana', '22/5/2001']]
csv.register_dialect('myDialect',
delimiter = '|',
quoting=csv.QUOTE_NONE,
skipinitialspace=True)
with open('dob.csv', 'w') as f:
writer = csv.writer(f, dialect='myDialect')
for row in person:
writer.writerow(row)
f.close()
A delimiter is a string used to separate fields. The default value is comma(,).
You can use pandas
. It does take some extra memory so it's not always possible, but it's very fast and easy to use.
import pandas as pd
pd.DataFrame(np_array).to_csv("path/to/file.csv")
if you don't want a header or index, use to_csv("/path/to/file.csv", header=None, index=None)