Converting Numpy Array to CSV

Converting Numpy Array to CSV

In data science and machine learning projects, it is common to work with Numpy arrays to store and manipulate numerical data. Sometimes, it is necessary to save these arrays to a CSV file for further analysis or sharing with others. In this article, we will explore how to convert Numpy arrays to CSV files in Python using the numpy and pandas libraries.

Using numpy.savetxt()

One way to save a Numpy array to a CSV file is by using the numpy.savetxt() function. This function allows you to specify the file name, delimiter, and formatting options.

import numpy as np

# Create a random Numpy  array
arr = np.random.rand(5, 3)

# Save the array to a CSV file
np.savetxt('data-numpywhere.csv', arr, delimiter=',')

After executing the code, a file data-numpywhere.csv is generated, data-numpywhere.csv Output:

Converting Numpy Array to CSV

Using pandas.DataFrame.to_csv()

Another way to convert a Numpy array to a CSV file is by first converting the array to a pandas DataFrame and then using the to_csv() method.

import pandas as pd
import numpy as np

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

# Convert array to DataFrame
df = pd.DataFrame(arr)

# Save DataFrame to CSV file
df.to_csv('data-numpywhere.csv', index=False)

After executing the code, a file data-numpywhere.csv is generated, data-numpywhere.csv Output:

Converting Numpy Array to CSV

Handling Missing Data

When dealing with Numpy arrays that have missing values, we can use the numpy.savetxt() function with the fmt parameter to set a placeholder for missing values.

import numpy as np

# Create a Numpy array with missing values
arr = np.array([[1, np.nan, 3], [4, 5, np.nan], [7, 8, 9]])

# Save the array to a CSV file with a placeholder for missing values
np.savetxt('data_missing.csv', arr, delimiter=',', fmt='%f', comments='', header=' '.join(map(str, arr.shape)))

After executing the code, a file data_missing.csv is generated, data_missing.csv Output:

Converting Numpy Array to CSV

Specifying Data Types

By default, Numpy arrays are saved as floats in CSV files. To specify a different data type, we can use the dtype parameter in the numpy.savetxt() function.

import numpy as np

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

# Save the array to a CSV file with integer type
np.savetxt('data_int.csv', arr, delimiter=',', fmt='%d')

After executing the code, a file data_int.csv is generated, data_int.csv Output:

Converting Numpy Array to CSV

Adding a Header

To add a header row to the CSV file, we can use the header parameter in the numpy.savetxt() function.

import numpy as np

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

# Save the array to a CSV file with a header
np.savetxt('data_header.csv', arr, delimiter=',', header='A,B,C', comments='')

After executing the code, a file data_header.csv is generated, data_header.csv Output:

Converting Numpy Array to CSV

Handling Date and Time

If the Numpy array contains datetime values, we can format them using the datetime module before saving to a CSV file.

import numpy as np
import datetime

# Create a Numpy  array with datetime values
arr = np.array([
    [datetime.datetime(2024, 1, 1), 100],
    [datetime.datetime(2023, 1, 2), 200],
    [datetime.datetime(2022, 1, 3), 300]
])

# Save the array to a CSV file with formatted datetime
np.savetxt('data-numpywhere.csv', arr, delimiter=',', fmt='%s')

File data-numpywhere.csv Output:

Converting Numpy Array to CSV

Custom Delimiter

By default, the delimiter in a CSV file is a comma. We can specify a custom delimiter using the delimiter parameter in the numpy.savetxt() function.

import numpy as np

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

# Save the array to a CSV file with a custom delimiter
np.savetxt('data_pipe.csv', arr, delimiter='|', fmt='%d')

File data_pipe.csv Output:

Converting Numpy Array to CSV

Appending Data

To append data to an existing CSV file, we can use the numpy.savetxt() function with the mode parameter set to a for append.

import numpy as np

# Create a Numpy array to append
arr_append = np.array([[10, 11, 12], [13, 14, 15]])

# Append the array to an existing CSV file
np.savetxt('data_append.csv', arr_append, delimiter=',', fmt='%d', comments='', header='')

File data_append.csv Output:

Converting Numpy Array to CSV

Skipping Rows and Columns

To skip rows or columns when saving a Numpy array to a CSV file, we can use the skiprows and usecols parameters in the numpy.savetxt() function.

import numpy as np

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

# Save the array to a CSV file without the first row and last column
np.savetxt('data_skip.csv', arr, delimiter=',', fmt='%d', skiprows=1, usecols=(0, 1))

File data_skip.csv Output:

Converting Numpy Array to CSV

Truncating Data

Sometimes we may want to truncate the decimal places in the CSV file. We can use the numpy.around() function to round the values before saving the array.

import numpy as np

# Create a Numpy  array with decimal values
arr = np.array([[1.23456789, 2.34567890], [3.45678901, 4.56789012]])

# Save the array to a CSV file with truncated decimal places
np.savetxt('data_truncate.csv', np.around(arr, decimals=2), delimiter=',', fmt='%.2f')

File data_truncate.csv Output:

Converting Numpy Array to CSV

Conclusion of converting Numpy array to CSV

In this article, we have explored different methods to convert Numpy arrays to CSV files in Python. By using the numpy.savetxt() function and manipulating array data, we can save numerical data in a format that can be easily shared and analyzed. By customizing the saving options, such as delimiter, data type, header, and formatting, we can efficiently export Numpy arrays to CSV files for various use cases in data science and machine learning applications.

Remember to adjust the code examples above based on your specific needs and data structures. Experiment with different parameters and functionalities to optimize the conversion process from Numpy arrays to CSV files.

Like(1)