How to write NumPy array to CSV file

You can write NumPy array to CSV file with the following code. If you want to learn Python, I highly recommend reading This Book.

How to write NumPy array to csv file
How to write NumPy array to CSV file

Method 1

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

np.savetxt("file.csv", a, delimiter=",")

Output:

Method 2

import pandas as pd
import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

pd.DataFrame(a).to_csv("file2.csv")

Output:

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.