Program to convert a NumPy array into a CSV file

In this python numpy program, we will convert a NumPy array into a CSV file.

Steps to solve the program
  1. Import the numpy library as np.
  2. Import the pandas library as pd
  3. Create an array using np.array().
  4. Convert the array to a matrix form using reshape().
  5. Convert the matrix to a data-frame using pd.DataFrame().
  6. Now save this data-frame as csv file using to_csv().
  7. Read the saved csv file using pd.read_csv().
  8. Print the file.
				
					import pandas as pd
import numpy as np
 
x = np.arange(1,16).reshape(3,5)
 
DF = pd.DataFrame(x)
DF.to_csv("data1.csv")

df=pd.read_csv("data1.csv")
print(df)
				
			

Output :

				
					   Unnamed: 0   0   1   2   3   4
0           0   1   2   3   4   5
1           1   6   7   8   9  10
2           2  11  12  13  14  15
				
			

calculate the product of an array.

access first two columns of a 3-D array.

Leave a Comment