How to Write Array to CSV File in Python [4 Ways]

Muhammad Waiz Khan Feb 12, 2024
  1. Use the numpy.savetxt() Method to Write an Array to a CSV File in Python
  2. Use the DataFrame.to_csv() Method to Write an Array to a CSV File in Python
  3. Use the writer.writerows() Method to Write an Array to a CSV File in Python
  4. Use File-Handling Methods to Write Array to CSV File in Python
  5. Conclusion
How to Write Array to CSV File in Python [4 Ways]

Working with data in Python often involves the need to save arrays for future use or analysis. One simple file format for storing tabular data is the Comma-Separated Values (CSV) file.

These files are easy to create and widely compatible with various applications.

Suppose we have an array containing some processed data, and we want to save them for future use; we can save them in a CSV file, as its format allows data to be saved in a structured table form. In this tutorial, we will look into various methods to write the array’s data to a CSV file in Python.

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

NumPy is a powerful library for numerical computations in Python. It provides a data structure, specifically the NumPy array, for efficient storage and manipulation of large arrays of data.

Before using NumPy, you’ll need to make sure it’s installed on your system. You can install it using pip:

pip install numpy

Once installed, you can import NumPy into your Python script using the following:

import numpy as np

This line of code imports the NumPy library and gives it the alias np for convenience.

To write NumPy arrays and save them to CSV files, NumPy’s **numpy.savetxt() method** offers a convenient and efficient way to accomplish this task.

Syntax:

numpy.savetxt(
    fname,
    array,
    fmt="%.18e",
    delimiter=" ",
    newline="\n",
    header="",
    footer="",
    comments="# ",
    encoding=None,
)
  • fname: The name of the file or a file-like object.
  • array: The array to be saved.
  • fmt: A format string for the array elements.
  • delimiter: The string used to separate values. By default, it is a space (' '), but it can be set to any character, such as a comma (','), tab ('\t'), or semicolon (';').
  • newline: String or character separating lines.
  • header: String that will be written at the beginning of the file.
  • footer: String that will be written at the end of the file.
  • comments: String that will be prepended to the header and footer strings.
  • encoding: The encoding used to encode the output file.

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 examples demonstrate how to use the numpy.savetxt() to write an array to a CSV file in Python.

Example 1: Writing a 1-D Array to CSV

import numpy as np

# Create a 1-D array
data = np.array([1, 2, 3, 4, 5])

# Save the array to a CSV file
np.savetxt("output.csv", data, delimiter=",")

Here’s a breakdown of the code:

The NumPy library is imported using import numpy as np.

A 1-dimensional array named data is created, containing the numbers 1 through 5.

The np.savetxt() function is used to save the data array to a CSV file named output.csv. The function parameters include the file name, the data array, and a comma delimiter.

Output (output.csv):

python write arrray to csv output 1

Keep in mind that depending on your system’s settings, you might see slightly different formatting. For example, the numbers might be rounded to a different number of decimal places.

Example 2: Writing a 2-D Array to CSV

import numpy as np

# Sample array
data = np.array(
    [
        ["Name", "Age", "Location"],
        ["John Doe", 30, "New York"],
        ["Jane Smith", 25, "Los Angeles"],
    ]
)

# Define the file name
file_name = "myfile.csv"

# Writing to CSV file
np.savetxt(file_name, data, delimiter=",", fmt="%s")

Here’s a breakdown of the code:

The NumPy library is imported with the alias np.

A 2-dimensional array named data is created. It contains information about names, ages, and locations.

A variable file_name is set to 'myfile.csv', which will be the name of the file to be saved.

The np.savetxt() function is used to save the data array to a CSV file named myfile.csv. The function parameters include the file name, the data array, a comma delimiter, and a format specifier indicating that all values should be treated as strings.

Output (myfile.csv):

python write arrray to csv output 2

Use the DataFrame.to_csv() Method to Write an Array to a CSV File in Python

Pandas is a widely used library for data manipulation and analysis in Python. It provides powerful tools for reading, writing, and analyzing data, with CSV handling being one of its strengths.

Before using Pandas, you’ll need to make sure it’s installed on your system. You can install it using pip:

pip install pandas

Once installed, you can import Pandas into your Python script using the following:

import pandas as pd

This line of code imports the Pandas library and gives it the alias pd for convenience.

DataFrame.to_csv() method is provided by the Pandas library for saving a DataFrame to a CSV file. It is used to export tabular data in a DataFrame to a comma-separated values (CSV) file format.

Syntax:

DataFrame.to_csv(
    path_or_buf, sep=", ", na_rep="", index=True, header=True, encoding=None
)
  • path_or_buf: The file path or object to write. If not specified, the result is returned as a string.
  • sep: The delimiter to use. The default is comma (',').
  • na_rep: The string representation of a missing value. The default is an empty string.
  • index: Whether to write row (index) labels. The default is True.
  • header: Whether to write the column names. The default is True.
  • encoding: The encoding to use for the output file.

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 CSV files in Python.

import pandas as pd

# Sample array
data = {
    "Name": ["John Doe", "Jane Smith"],
    "Age": [30, 25],
    "Location": ["New York", "Los Angeles"],
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Writing to CSV file
df.to_csv("myfile.csv", index=False)

Here’s a breakdown of the code:

We import the Pandas library using import pandas as pd.

Then, we create a DataFrame named df from a dictionary data containing information about names, ages, and locations.

We use the to_csv() method to write the DataFrame df to a CSV file named myfile.csv. The index=False argument ensures that the row labels are not included in the CSV.

The code above will generate a file named myfile.csv with the contents of the DataFrame df. A comma will separate each column, and there won’t be any row labels.

Output (myfile.csv):

python write arrray to csv output 3

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

The CSV module in Python provides functionalities in both reading from and writing CSV files. It simplifies the process of handling CSV data, allowing you to work with structured data directly.

The CSV module primarily consists of two key components:

  • csv.reader(): This class allows you to read data from a CSV file row by row, returning each row as a list.
  • csv.writer(): This class provides methods to write data to a CSV file, including the writerows() method, which allows you to write multiple rows at once.

The CSV module is part of Python’s standard library, so there’s no need to install it separately. It comes pre-installed with Python.

To use the CSV module in your Python script, you need to import it. This can be done with the following line of code:

import csv

This line of code imports the CSV module, allowing you to use its functionalities.

The writerows() method is a part of the csv.writer() class. It allows you to write multiple rows of data to a CSV file in a single operation.

This method is highly efficient when dealing with large datasets.

Syntax:

writer.writerows(rows)
  • writer: The CSV writer object.
  • rows: An iterable (e.g., a list of lists, a generator, etc.) where each element represents a row of data. Each row itself should be represented as a list (or tuple) of values.

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

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

# Writing to CSV file
with open("myfile.csv", "w", newline="") as file:
    mywriter = csv.writer(file, delimiter=",")
    mywriter.writerows(a)

Here’s what the code does:

  • It imports the necessary modules: csv and numpy.
  • It creates a NumPy array named a with sample data.
  • It opens a CSV file named 'myfile.csv' in write mode ('w') using a context manager (with open(...) as ...). The newline='' argument is provided to ensure compatibility across different platforms.
  • It creates a csv.writer object named mywriter with the specified delimiter (,).
  • It uses mywriter.writerows(a) to write the contents of the NumPy array a to the CSV file.

Overall, the code above effectively uses NumPy and the CSV module to write a NumPy array to a CSV file named 'myfile.csv'.

Output (myfile.csv):

python write arrray to csv output 4

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)

Here’s what the code does:

  • It imports the csv module.
  • It opens the CSV file named 'myfile.csv' in read mode ('r') using a context manager (with open(...) as ...). The newline='' argument is provided to ensure compatibility across different platforms.
  • It creates a csv.reader object named myreader with the specified delimiter (,).
  • It uses a for loop to iterate through the rows of the CSV file.
  • Within the loop, each row is printed.

Output:

python write arrray to csv output 5

Use File-Handling Methods to Write Array to CSV File in Python

Using file-handling methods provides a direct and versatile approach to writing data to CSV files, including modifying an existing file. It offers fine-grained control over the writing process, making it suitable for a wide range of scenarios.

You can use standard file-handling methods to write arrays to a CSV file manually. We’ll use Python’s built-in open() function and file operations to achieve this.

The open() function is used to open files in Python. It takes two arguments: the file path and the mode in which the file is to be opened (e.g., read, write, append, etc.).

file = open("data.csv", "w")

Using a with statement ensures that a file is properly closed after its suite finishes, even if an error occurs.

with open("data.csv", "w") as file:
    # Code to write data to the file
    pass

Example 1: Writing 1-Dimensional Arrays

data = [1, 2, 3, 4, 5]

with open("output.csv", "w") as file:
    for item in data:
        file.write(f"{item}\n")

We have a 1-dimensional array called data containing the numbers 1 through 5.

The with open('output.csv', 'w') as file statement opens a file named output.csv in write mode. The with statement ensures that the file will be properly closed after writing, even if an error occurs.

Inside the with block, we loop through each item in the data array. For each item, we use file.write() to write it to the file.

The f'{item}\n' syntax formats the item as a string and adds a newline character (\n) to separate the values. The code above will generate a file named output.csv with each number from the data array on a separate line.

Output (output.csv):

python write arrray to csv output 6

Example 2: Writing 2-Dimensional Arrays

# Sample array
data = [
    ["Name", "Age", "Location"],
    ["John Doe", 30, "New York"],
    ["Jane Smith", 25, "Los Angeles"],
]

# Define the file name
file_name = "myfile.csv"

# Writing to CSV file
with open(file_name, mode="w", newline="") as file:
    for row in data:
        line = ",".join(map(str, row)) + "\n"
        file.write(line)

We have a 2-dimensional array named data. This array contains information about names, ages, and locations.

The variable file_name is set to 'myfile.csv', which is the name of the file you want to save.

The with open(...) statement opens the file in write mode ('w'). The newline='' argument ensures that newlines are handled correctly.

Inside the with block, we loop through each row in the data array. For each row, we use ','.join(map(str, row)) + '\n' to convert the row elements to strings, join them with commas, and add a newline character (\n). This forms a CSV line.

The file.write(line) writes each line to the file.

The code above will generate a file named myfile.csv with the content of the data array formatted as a CSV.

Output (myfile.csv):

python write arrray to csv output 7

By using this method, we manually control the process of writing to the CSV instead of a text file.

This provides more flexibility but requires more manual handling compared to using dedicated CSV writing libraries like csv.writer, pandas, or numpy. However, it’s a useful technique if you need specific control over the writing process.

Conclusion

Handling data in Python often involves the need to save arrays in a CSV format. We explored various methods to achieve this, including using numpy.savetxt() from NumPy, DataFrame.to_csv() from Pandas, csv.writer() and writer.writerows() from Python’s CSV module, and file-handling methods.

Choose the method based on your specific needs, considering factors like data size and customization requirements. With these techniques, you can confidently handle and convert array data in CSV data for a variety of applications.

Related Article - Python Array