Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Dump a NumPy array into a csv file

Is there an approach to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in a human-readable format.
by

2 Answers

aashaykumar
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)
kshitijrana14
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=",")

Login / Signup to Answer the Question.