Numpy Savetxt: How to save Numpy Array to text and CSV File

Save Numpy Array to text and CSV File using numpy savetxt method

Numpy Savetxt is a method to save an array to a text file or CSV file. In this coding tutorial, I will show you the implementation of the NumPy savetxt() method using the best examples I have compiled.

First of all, let’s import all the necessary libraries required for this tutorial. Here I am using only NumPy library.

import numpy as np

How to Save One Dimensional  Numpy array using numpy.savetxt()

In this section, I will create a one-dimensional NumPy array. Then after saving it to both as a text file and CSV file.

array_1d = np.array([10,30,40,20])

Saving to text file

np.savetxt("array_1d.txt",array_1d,delimiter=",")

Here I am passing the filename, the array I want to save and delimiter to split the array elements after the comma.

Output

Saving 1D Numpy Array to text file
Saving 1D Numpy Array to a text file

Saving to a CSV File

You can save it to a CSV file by just changing the name of the file.

np.savetxt("array_1d.csv",[array_1d],delimiter=",",fmt="%d")

There is something you should be careful about.  You have to pass your 1D Numpy array inside the square bracket. And fmt=”%d” as by default the array will be stored as float type. Here We are using the values of integer type.

Output

Saving 1D Numpy Array to a CSV file
Saving 1D Numpy Array to a CSV file

You can also add a header and footer argument inside the np.savetxt() method. Just execute the following code.

np.savetxt("array_1d_with_hf.csv",[array_1d],delimiter=",",fmt="%d",header="This is header",footer="This is footer")

Output

Saving 1D Numpy Array to a CSV file with header and footer
Saving 1D Numpy Array to a CSV file with header and footer

Save Two Dimensional  Numpy array using numpy.savetxt()

The above example was for one dimensional array. Now let’s save the two-dimensional array as a text and CSV file. Let’s create a Two Dimensional Array.

array_2d = np.array([[10,30,20],[60,50,40],[5,6,7]])

Saving to a text file

np.savetxt("array_2d.txt",array_2d,delimiter=",")

Output

Saving 2D Numpy Array to text file
Saving 2D Numpy Array to text file

Saving to a CSV File

np.savetxt("array_2d.csv",array_2d,delimiter=",",fmt="%d")

Output

Saving 2D Numpy Array to CSV file
Saving 2D Numpy Array to CSV file

You can also see how I am not the square bracket for array_2d as here it is not required.

How to Save a Structured Numpy array in CSV file?

You can also save a Structured Numpy array to a CSV file. Those Numpy arrays that has a custom data type (dtype) is a structured Numpy array. Below is the code for the Structured Numpy array.

#type of the numpy array structure
dtype = [('Name', (np.str_, 10)), ('RollNo', np.int32), ('Marks', np.float64)]
strud_array = np.array([("Sahil",15,92),("Abhishek",16,98),("John",17,100)],dtype=dtype)

Here I am defining the name, roll no, and marks with their corresponding type.

Now you can save the strud_array with the header as Name, RollNo, and Marks. It will act as the column name in your CSV File.

np.savetxt('strud_array.csv', strud_array, delimiter=',', fmt=['%s' , '%d', '%f'], header='Name,RollNo,Marks', comments='')

Output

Saving Structured Numpy Array to CSV file
Saving Structured Numpy Array to CSV file

Save more than one NumPy array

In this section, I will show you how you can save more than one Numpy array to both text file and CSV file.

Let’s create three Numpy array.

array1 = np.arange(100,200,10)
array2 = np.arange(200,300,10)
array3 = np.arange(300,400,10)

Save to text file

np.savetxt("3_array.txt",(array1,array2,array3))

Output

Saving Three Numpy Array to text file
Saving Three Numpy Array to text file

Saving to CSV File

np.savetxt("3_array.csv",(array1,array2,array3),delimiter=",",fmt="%d")

Output

Saving Three Numpy Array to CSV file
Saving Three Numpy Array to CSV file

Here In both cases, I am passing the set of Numpy array. All arrays will be appended to the next row of the file.

 

Conclusion

Numpy savetxt method is very useful for saving and retrieving your own dataset. You can manipulate or change any existing dataset and save it. These are examples I have coded for getting a deep understanding. If you have any queries then you can contact us for more information.

Source:

Official Numpy Savetxt Documentation

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner