# How To Write Numpy Array To A CSV file In Python – Definitive Guide

Numpy Arrays are used to store data in array format.

You can use the `savetxt()` method to write a numPy array to a CSV file.

Basic Example

``````import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.savetxt('myArrayAsCSV.csv', numpyArr, delimiter=",")``````
• It writes data in `“%.18e”` format by default, with 18 zeros.

This tutorial teaches you the different methods to write NumPy array to a CSV file in Python and when it is appropriate to use them.

## Using NumPy Savetxt

The savetxt() method writes the NumPy array into a CSV file.

• Pass the CSV file name, array and the delimiter to delimit the data.
• It writes data in `“%.18e”` format by default if the `fmt` parameter is not specified.

Use this method when you want to save the array in a CSV file and want the data in the `“%.18e”` format to maintain the precision of the data.

Code

``````import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.savetxt('myArrayAsCSV.csv', numpyArr, delimiter=",")``````

Output

``````1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00
4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00
7.000000000000000000e+00,8.000000000000000000e+00,9.000000000000000000e+00``````

## Using NumPy Savetxt with Format Specification

This section teaches you how to use the `savetxt()` method with the format specification.

It is specifically useful when you want to format the data with fewer decimals or in a string format.

• Use the `fmt` parameter and specify the format. A string or a sequence of formats. Example: “%d” to format the numbers as decimals instead of float.

Use this method when you want to store the numpy array data in a decimal or a string format.

Code

``````import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

np.savetxt('myArrayAsCSV.csv', numpyArr, fmt="%d", delimiter=",")``````

Output

``````1,2,3
4,5,6
7,8,9``````

## Using Numpy To_File

The to_file() method writes the numpy array as a text file or a binary file.

• It is a useful and convenient method for quick storage of the array of data.
• It doesn’t store the endianness, and precision is lost.
• Hence, it is not recommended to use it to create files for data archival or data transportation purposes.

Use this method when you want to store and retrieve the array data quickly and don’t want to retain the precision of the data.

Code

``````import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

numpyArr.tofile('myArrayAsCSV.csv',sep=',',format="%d")``````

Output

``1,2,3,4,5,6,7,8,9``

## Save Numpy Array to CSV with Headers

To convert the numpy array to a CSV file with headers,

• Use the `savetxt()` method
• Use the `header` parameter with the list of headers. The length of the headers must be equal to the number of columns in the array
• Pass the `comments=“”` parameter to avoid `#` at the beginning of the header information

Code

``````import numpy as np

numpyArr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

numpyArr.tofile('myArrayAsCSV.csv',sep=',',format="%d")

np.savetxt("myArrayAsCSV.csv", numpyArr, delimiter=",", fmt="%d",
``````A, B, C