Convert a NumPy array into a csv file


The Numpy is the library in the python programming language which is abbreviated as Numerical Python. It is used to do the mathematical, scientific and statistical calculations within less time. The output of the numpy functions will be an array. An array created by the numpy functions can be stored in the CSV file using a function namely, savetxt().

Numpy array into a .csv file

CSV is abbreviated as Comma Separated Values. This is the file format most widely used in the Data Science. This stores the data in a tabular format where the column holds the data fields and rows holds the data.

Each row in the .csv file is separated by a comma or by a delimiter character which can be customized by the user. We have to use the pandas library to work with the csv files in data science.

The data from the csv file can also be revived into the numpy array format using the function loadtxt() of the numpy library.

Syntax

Following is the syntax for converting the numpy array into the csv file using the savetxt() function.

numpy.savetxt(file_name,array,delimiter = ‘,’)

Where,

  • numpy is the name of the library.

  • savetxt is the function used to convert the array into .csv file.

  • file_name is the name of the csv file.

  • array is the input array which need to be converted into .csv file.

Example

In order to convert the array elements into the csv file, we have to pass the name of the file and input array as the input arguments to the savetxt() file along with the delimiter = ‘,’ , then the array will be transformed into the csv file. Following is the example for the same.

import numpy as np
a = np.array([23,34,65,78,45,90])
print("The created array:",a)
csv_data = np.savetxt("array.csv",a,delimiter = ",")
print("array converted into csv file")
data_csv = np.loadtxt("array.csv",delimiter = ",")
print("The data from the csv file:",data_csv)

Output

The created array: [23 34 65 78 45 90]
array converted into csv file
The data from the csv file: [23. 34. 65. 78. 45. 90.]

Example

Let’s see another example where we convert the 2D array data into a CSV file using the savetxt() function of the numpy library.

import numpy as np
a = np.array([[23,34,65],[78,45,90]])
print("The created array:",a)
csv_data = np.savetxt("2d_array.csv",a,delimiter = ",")
print("array converted into csv file")
data_csv = np.loadtxt("array.csv",delimiter = ",")
print("The data from the csv file:",data_csv)

Output

The created array: [[23 34 65]
[78 45 90]]
array converted into csv file
The data from the csv file: [[23. 34. 65.]
[78. 45. 90.]]

Example

In the following example we are using “_” as a delimiter of the CSV file instead of “,”.

import numpy as np
a = np.array([[23,34,65,87,3,4],[78,45,90,53,5,3]],int)
print("The created array:",a)
csv_data = np.savetxt("2d_array.csv",a,delimiter = "_")
print("array converted into csv file")
data_csv = np.loadtxt("2d_array.csv",delimiter = "_")
print("The data from the csv file:",data_csv)

Output

Following is the output of the array converted into the csv file.

The created array: [[23 34 65 87 3 4]
[78 45 90 53 5 3]]
array converted into csv file
The data from the csv file: [[23. 34. 65.]
[78. 45. 90.]]

Updated on: 09-Aug-2023

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements