Dump a NumPy array into a csv file
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping
--
Chapters
00:00 Dump A Numpy Array Into A Csv File
00:16 Accepted Answer Score 1250
00:33 Answer 2 Score 235
00:54 Answer 3 Score 60
01:48 Answer 4 Score 25
03:06 Answer 5 Score 22
04:02 Thank you
--
Full question
https://stackoverflow.com/questions/6081...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #csv #numpy
#avk47
ACCEPTED ANSWER
Score 1257
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=",")
ANSWER 2
Score 236
Use the pandas library's DataFrame.to_csv. It does take some extra memory, but it's very fast and easy to use.
import pandas as pd
df = pd.DataFrame(np_array)
df.to_csv("path/to/file.csv")
If you don't want a header or index, use:
df.to_csv("path/to/file.csv", header=False, index=False)
ANSWER 3
Score 61
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.
ANSWER 4
Score 22
Writing record arrays as CSV files with headers requires a bit more work.
This example reads from a CSV file (example.csv) and writes its contents to another CSV file (out.csv).
import numpy as np
# Write an example CSV file with headers on first line
with open('example.csv', 'w') as fp:
fp.write('''\
col1,col2,col3
1,100.1,string1
2,222.2,second string
''')
# Read it as a Numpy record array
ar = np.recfromcsv('example.csv', encoding='ascii')
print(repr(ar))
# rec.array([(1, 100.1, 'string1'), (2, 222.2, 'second string')],
# dtype=[('col1', '<i8'), ('col2', '<f8'), ('col3', '<U13')])
# Write as a CSV file with headers on first line
with open('out.csv', 'w') as fp:
fp.write(','.join(ar.dtype.names) + '\n')
np.savetxt(fp, ar, '%s', ',')
Note that the above example cannot handle values which are strings with commas. To always enclose non-numeric values within quotes, use the csv built-in module:
import csv
with open('out2.csv', 'w', newline='') as fp:
writer = csv.writer(fp, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(ar.dtype.names)
writer.writerows(ar.tolist())