In this article we will discuss how to save 1D & 2D Numpy arrays in a CSV file with or without header and footer.

numpy.savetxt()

Python’s Numpy module provides a function to save numpy array to a txt file with custom delimiters and other custom options i.e.

Arguments:

  • arr : 1D or 2D numpy array (to be saved)
  • fmt : A formatting pattern or sequence of patterns, that will be used while saving elements to file.
    • If a single formatter is specified like ‘%d’ then it will be applied to all elements.
    • In case of 2D arrays, a list of specifier i.e. different for each column. (Optional)
  • delimiter : String or character to be used as element separator (Optional)
  • newline : String or character to be used as line separator (Optional)
  • header : String to be written at the beginning of the txt file.
  • footer : String to be written at the end of the txt file.
  • comments : Custom comment marker , default is ‘#’. Will be pre-appended to the header and footer.

Let’s use this to save 1D and 2D numpy arrays to a csv file.

Save Numpy array to CSV File using using numpy.savetxt()

First of all import Numpy module i.e.

Now suppose we have a 1D Numpy array i.e.

It will save this numpy array to csv file with name ‘array.csv‘. Contents of this file will be like,

It will create a csv file with name ‘array.csv’ and store contents of numpy array in it i.e.

We passed the delimiter ‘,’ to make it in csv format. Also passed the format string as ‘%d’, so that it can store elements as integer. By default it will store numbers in float format. Also not that if you don’t surround numpy array by this [] i.e. convert it to list while passing it to numpy.savetxt() then comma delimiter will not work, it will use ‘\n’ as delimiter by default. So, surrounding array by [] i.e. [arr] is must.

Save 1D Numpy array to csv file with Header and Footer

If you want to add comments in header and footer while saving the numpy array to csv file, then we can pass the header and footer parameters i.e.

It will create a file with name ‘array_hf.csv‘ and following contents,

By default comments in both header and footer will be pre-appended by ‘#’. If we want to change this then we can pass the parameter comments like comments=’@’

Save 2D Numpy array to CSV File

Let’s create a 2D numpy array from list of list i.e.

Contents of this 2D numpy array is,

Now let’s save this 2D numpy array to csv file with name ‘2darray.csv‘ i.e.

Contents of the file ‘2darray.csv’ will be,

Also, instead of saving complete 2D numpy array to a csv file, if we want we can save single or multiple columns or rows only.

Save a column of 2D numpy array to csv file

Let’s check contents of the file ‘2darray_column.csv‘,

Save a row of 2D numpy array to csv file

Let’s check contents of the file ‘2darray_row.csv‘,

Save a Structured Numpy array in csv file by using list of formatting options

Let’s create a structured numpy array where each element in numpy array will contain a string, float and integer i.e.

Contents of this structured numpy array is,

Now to store this structured numpy array to csv file using savetxt() we need to pass list of formatting options i.e.

Contents of the file ‘struct_array.csv‘ will be,

As each element in our numpy array was a combination of string, float and integer therefore while saving it to csv file we pass the formatting options as =[‘%s’ , ‘%f’, ‘%d’]

Complete example is as follows:

Output:

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.