NumPy
CSV file
Python programming
data processing
data export

Dump a NumPy array into a csv file

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Writing a NumPy array to CSV is a common export step when you want to inspect numeric data outside Python or hand it to another tool. The simplest solution for plain numeric arrays is usually numpy.savetxt. If you need headers, indexes, or more table-like behavior, converting to a Pandas DataFrame and using to_csv is often more convenient.

The Straight NumPy Solution: savetxt

For numeric arrays, numpy.savetxt is the standard direct tool.

python
1import numpy as np
2
3array = np.array([
4    [1.5, 2.5, 3.5],
5    [4.5, 5.5, 6.5]
6])
7
8np.savetxt('output.csv', array, delimiter=',')

This writes a comma-separated file with one row per array row.

If you need a specific numeric format, use fmt.

python
np.savetxt('output.csv', array, delimiter=',', fmt='%.2f')

That prevents the default scientific or high-precision formatting from making the CSV harder to read.

Writing Integer Arrays

If the data is integer-based, match the format string to the data.

python
1import numpy as np
2
3array = np.array([[1, 2, 3], [4, 5, 6]])
4np.savetxt('ints.csv', array, delimiter=',', fmt='%d')

Using %d avoids writing integers as floating-point values.

Add a Header When Needed

CSV exports often need column names.

python
1import numpy as np
2
3array = np.array([[1.1, 2.2], [3.3, 4.4]])
4np.savetxt(
5    'with_header.csv',
6    array,
7    delimiter=',',
8    header='feature_a,feature_b',
9    comments=''
10)

The comments='' part prevents NumPy from prefixing the header with #, which many CSV consumers do not want.

Use Pandas for More Table-Like Output

If you need labels, indexes, or more control over CSV structure, Pandas is often the better tool.

python
1import numpy as np
2import pandas as pd
3
4array = np.array([[10, 20, 30], [40, 50, 60]])
5df = pd.DataFrame(array, columns=['x', 'y', 'z'])
6df.to_csv('output_pandas.csv', index=False)

This is especially useful when the array is really tabular data rather than just raw matrix output.

Choose Delimiters and Formats Deliberately

CSV usually means commas, but many tools expect semicolons, tabs, or locale-specific decimal conventions. savetxt can handle those cases too.

python
1import numpy as np
2
3array = np.array([[1.25, 2.50], [3.75, 4.00]])
4np.savetxt('output_semicolon.csv', array, delimiter=';', fmt='%.3f')

This is useful when the receiving system is not using the default comma-separated convention.

One-Dimensional Arrays Need Attention Too

A one-dimensional array can be written directly, but the layout may not match what you expect.

python
1import numpy as np
2
3arr = np.array([1, 2, 3, 4])
4np.savetxt('vector.csv', arr, delimiter=',', fmt='%d')

This writes one value per line. If you want a single CSV row, reshape first.

python
np.savetxt('vector_row.csv', arr.reshape(1, -1), delimiter=',', fmt='%d')

Validate the Export by Reading It Back

A quick read-back check is a good way to catch delimiter or formatting mistakes.

python
1import numpy as np
2
3loaded = np.loadtxt('output.csv', delimiter=',')
4print(loaded)

That is especially useful when another system is going to consume the file later.

Common Pitfalls

  • Using savetxt without fmt and getting numeric formatting that is harder to read than expected.
  • Forgetting that one-dimensional arrays write as one value per line unless reshaped.
  • Writing integer data with a floating-point format or vice versa.
  • Expecting CSV headers from NumPy without supplying them explicitly.
  • Using raw NumPy output when the real need is a labeled table better handled by Pandas.

Summary

  • Use numpy.savetxt for direct CSV export of numeric arrays.
  • Set delimiter=',' and choose an appropriate fmt for readable output.
  • Add headers explicitly if the CSV should have column names.
  • Reshape one-dimensional arrays if you want a single row instead of one value per line.
  • Prefer Pandas to_csv when labels and table-style export matter.

Course illustration
Course illustration

All Rights Reserved.