How can I export a NumPy array to a CSV file? 2

How can I export a NumPy array to a CSV file?

Exporting a NumPy array to a CSV file allows for the data to be easily shared and analyzed by others. This process involves using the NumPy library’s built-in function “np.savetxt()” to convert the array into a CSV file format. The function allows for customizable parameters such as delimiter, header, and precision to be specified. Once the array is converted, it can be saved as a CSV file using the “np.savetxt()” function. This allows for seamless integration of NumPy arrays into data analysis and visualization tools.

Export a NumPy Array to a CSV File (With Examples)


You can use the following basic syntax to export a NumPy array to a CSV file:

import numpy as np

#define NumPy array
data = np.array([[1,2,3],[4,5,6],[7,8,9]])

#export array to CSV file
np.savetxt("my_data.csv", data, delimiter=",")

The following examples show how to use this syntax in practice.

Example 1: Export NumPy Array to CSV

The following code shows how to export a NumPy array to a CSV file:

import numpy as np

#define NumPy array
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12], [13, 14, 15]])

#export array to CSV file
np.savetxt("my_data.csv", data, delimiter=",")

If I navigate to the location where the CSV file is saved on my laptop, I can view the data:

Example 2: Export NumPy Array to CSV With Specific Format

The default format for numbers is “%.18e” – this displays 18 zeros. However, we can use the fmt argument to specify a different format.

For example, the following code exports a NumPy array to CSV and specifies two decimal places:

import numpy as np

#define NumPy array
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12], [13, 14, 15]])

#export array to CSV file (using 2 decimal places)
np.savetxt("my_data.csv", data, delimiter=",", fmt="%.2f")

If I navigate to the location where the CSV file is saved, I can view the data:

Example 3: Export NumPy Array to CSV With Headers

The following code shows how to export a NumPy array to a CSV file with custom column headers:

import numpy as np

#define NumPy array
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12], [13, 14, 15]])

#export array to CSV file (using 2 decimal places)
np.savetxt("my_data.csv", data, delimiter=",", fmt="%.2f",
           header="A, B, C", comments="")

If I navigate to the location where the CSV file is saved, I can view the data:

Note: You can find the complete documentation for the numpy.savetxt() function .

The following tutorials explain how to perform other common read and write operations in Python:

Cite this article

stats writer (2024). How can I export a NumPy array to a CSV file?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-export-a-numpy-array-to-a-csv-file/

stats writer. "How can I export a NumPy array to a CSV file?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-export-a-numpy-array-to-a-csv-file/.

stats writer. "How can I export a NumPy array to a CSV file?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-export-a-numpy-array-to-a-csv-file/.

stats writer (2024) 'How can I export a NumPy array to a CSV file?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-export-a-numpy-array-to-a-csv-file/.

[1] stats writer, "How can I export a NumPy array to a CSV file?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I export a NumPy array to a CSV file?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top