In this python numpy program, we will convert a NumPy array into a CSV file.
Steps to solve the program
- Import the numpy library as np.
- Import the pandas library as pd
- Create an array using np.array().
- Convert the array to a matrix form using reshape().
- Convert the matrix to a data-frame using pd.DataFrame().
- Now save this data-frame as csv file using to_csv().
- Read the saved csv file using pd.read_csv().
- 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