Convert a NumPy array into a CSV using Dataframe.to_csv()
The DataFrame.to_csv() method is used to write a Dataframe into a CSV file.
To use DataFrame.to_csv() we have to first convert the NumPy array into pandas DataFrame and then save it to CSV format.
Example
Python3
import pandas as pd import numpy as np # create a dummy array arr = np.arange( 1 , 11 ).reshape( 2 , 5 ) print (arr) # convert array into dataframe DF = pd.DataFrame(arr) # save the dataframe as a csv file DF.to_csv( "data1.csv" ) |
Output:
Convert a NumPy array into a CSV file
After completing your data science or data analysis project, you might want to save the data or share it with others. Exporting a NumPy array to a CSV file is the most common way of sharing data.
CSV file format is the easiest and most useful format for storing data and is convenient to share with others. So in this tutorial, we will see different methods to convert a NumPy array into a CSV file.
Different methods to convert a NumPy array to a CSV file are:
- Using DataFrame.to_csv() method
- Using NumPy_array.tofile() method
- Using NumPy.savetxt() method
- Using File Handling operations
Let’s understand these methods better with examples.
Contact Us