Convert a NumPy array into a csv file
In this article, we are going to see different methods to save an NumPy array into a CSV file. CSV file format is the easiest and useful format for storing data
There are different methods by which we can save the NumPy array into a CSV file
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Method 1: Using Dataframe.to_csv().
This method is used to write a Dataframe into a CSV file.
Example: Converting the array into pandas Dataframe and then saving it to CSV format.
[[ 1 2 3 4 5] [ 6 7 8 9 10]]
Method 2: Using numpy_array.tofile().
This method is used to write an array into the file.
Example: Create an array then save into a CSV file.
[ 1 2 3 4 5 6 7 8 9 10]
Method 3: Using numpy.savetext().
This method is used to save an array to a text file.
Example: Create an array then save as a CSV file.