The world’s leading publication for data science, AI, and ML professionals.

How To Write NumPy Array Into CSV File

Showcasing how to dump NumPy arrays into .csv files

Photo by Kelly Sikkema on Unsplash
Photo by Kelly Sikkema on Unsplash

Introduction

When working with arrays in NumPy and Python, we usually need to dump them into output files of various forms, including comma-separated values (CSV).

In today’s short tutorial we will be discussing about a few different approaches one can use in order to write NumPy arrays into such files. Finally, we will discuss about one approach that you should typically avoid when it comes to dumping NumPy arrays into files.


First, let’s create a sample numpy array that we will be referencing throughout this short tutorial in order to demonstrate a few concepts.

import numpy as np

arr = np.asarray([ 
    [1, 150, 10.5], 
    [2, 220, 3.1], 
    [3, 121, 10.1],
    [4, 300, 3.2], 
    [5, 541, 6.7], 
    [6, 321, 9.9],
])
print(arr)
array([[  1. , 150. ,  10.5],
       [  2. , 220. ,   3.1],
       [  3. , 121. ,  10.1], 
       [  4. , 300. ,   3.2],
       [  5. , 541. ,   6.7],
       [  6. , 321. ,   9.9]])

Using numpy.savetxt() method

The first option we have when we need to dump a NumPy array into an output file is [numpy.savetxt()](https://numpy.org/doc/stable/reference/generated/numpy.savetxt.html) method that is used to save an array to a text file. The method allows one to specify the delimiter, which in our case should be a comma:

np.savetxt('output.csv', arr, delimiter=',')

Note that the output will use a scientific notation, as illustrated below.

Example output - Source: Author
Example output – Source: Author

If you would like to avoid using scientific notation, then you can specify an appropriate fmt. For example,

np.savetxt('output.csv', arr, delimiter=',', fmt='%f')
Example output without scientific notation - Source: Author
Example output without scientific notation – Source: Author

Using numpy.tofile() method

The second option is [numpy.ndarray.tofile()](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tofile.html) method that is used to write array to a file as text or binary. Once again, we can specify both the delimiter (sep) and format:

arr.tofile('output.csv', sep=',', format='%f')

What to avoid

Another popular (but not very smart) approach is to first convert the NumPy array into a pandas DataFrame and then use the [pandas.DataFrame.to_csv()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html) method in order to write the resulting DataFrame into the output csv file.

Note however, that this isn’t a good practise as it will consume too much memory -especially if you are working with fairly large arrays- for literally no reason.


Final Thoughts

In today’s short tutorial we showcased a few different approaches when it comes to writing NumPy arrays into CSV files. More specifically, we discussed how to do so using numpy.savetxt() and numpy.tofile() methods.

Finally, we highlighted the importance of avoiding to involve pandas in the process of dumping a NumPy array into a CSV file as this is going to be quite costly in terms of memory.


Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Join Medium with my referral link – Giorgos Myrianthous


Related articles you may also like

requirements.txt vs setup.py in Python


How To Merge Pandas DataFrames


How To Compute Euclidean Distance in NumPy


Related Articles