Write Array to CSV File in Python

  1. Python Write an Array to a CSV File in Python Using the numpy.savetxt() Method
  2. Python Write Array to a CSV File in Python Using the Dataframe.to_csv() Method
  3. Python Write Array to a CSV File in Python Using the writer.writerows() Method

In this tutorial, we will look into various methods to write the array’s data to a comma-separated values (CSV) file in Python. Suppose we have an array containing some processed data, the output of some algorithm, weights of some model, etc., and we want to save them for future use.

The CSV file is commonly used to save the array data, as its format allows data to be saved in a structured table form. We can write an array to a CSV file using the following methods in Python.

Python Write an Array to a CSV File in Python Using the numpy.savetxt() Method

The numpy.savetxt(fname, array, delimiter=) method saves the input array to the file - fname. The delimiter argument is the value used as the separator to separate the independent value in the data.

To save the array to a CSV file, we will need to pass the fname with the .csv extension and use the comma , as the separator. The below example code demonstrates how to use the numpy.savetxt() to write an array to a CSV file in Python.

import numpy as np

a = np.array([[1,4,2],[7,9,4],[0,6,2]])
np.savetxt('myfile.csv', a, delimiter=',')

Python Write Array to a CSV File in Python Using the Dataframe.to_csv() Method

The Dataframe.to_csv(path, sep,...) method saves a Dataframe to a file and path specified in the path argument. The sep argument is the separator or delimiter, which will be used to separate the values; the default value of the sep argument is comma ,.

We can write an array to a CSV file by first converting it to a Dataframe and then providing the CSV file’s path as the path argument using the Dataframe.to_csv() method. Since the default value of the sep argument is ,, we have to provide the Dataframe and the path argument to the Dataframe.to_csv() method.

The below example code demonstrates how to use the Dataframe.to_csv() method to write the array to a CSV file in Python.

import pandas as pd

df = pd.DataFrame(myarray)
df.to_csv('myfile.csv')

Python Write Array to a CSV File in Python Using the writer.writerows() Method

The writer.writerows(rows) takes the rows argument in the shape of a 2D array or list and writes it into the file object of the writer. The writer object is returned from the csv.writer(file) method, which takes a file object file as input and returns the writer object as output.

To write an array to a CSV file, we first pass the CSV file object to the csv.writer() method and then use the writer object returned from the csv.writer() method to write the array to the CSV file.

The below example code demonstrates how to use the writer.writerows() method to write the array to a CSV file in Python.

import csv
import numpy

a = numpy.array([[1,4,2],[7,9,4],[0,6,2]])

with open('myfile.csv', 'w', newline='') as file:
    mywriter = csv.writer(file, delimiter=',')
    mywriter.writerows(a)

To read the array from the CSV file, we can use the csv.reader(file) method’s writer object to read the array from the file object file of the CSV file in Python.

The below example code demonstrates how to use the csv.reader() method to read the array from the CSV file.

import csv

with open('myfile.csv', 'r', newline='') as file:
  myreader = csv.reader(file, delimiter=',')
  for rows in myreader:
    print(rows)

Output:

['1', '4', '2']
['7', '9', '4']
['0', '6', '2']
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Array

  • Get Length of a Python Array
  • Shuffle an Array in Python