Storing a NumPy array in CSV format

The savetxt() function admits several parameters, which are useful when we want to output the array in a format that is compatible with a specific application. In this recipe, we show you how to store a NumPy array in CSV format. Comma-separated values (CSV) are used to store tabular data in a text file. Each row of the table is stored in a line of text and elements in each row are separated by a comma. The following code illustrates how to save an array in CSV format:

m, n = 10, 5
x = np.random.rand(m, n)
columns = ','.join(['Column {}'.format(str(i+1)) for i in range(n)])
np.savetxt('array_x.csv', x, fmt='%10.8f',
delimiter=',', header=columns, comments='')

With this code, we first generate a 10 x 5 array x with random data. Next, we define a columns string containing the table headings, separated by commas. Finally, we save the array to the array_x.csv file. The options used in the call to savetxt() are described as follows:

  • fmt='%10.8f' specifies that each array element should be formatted as a floating-point value in a field with a width of 10 and a precision of 8 decimals.
  • delimiter=',' sets the field separator to be a comma.
  • header=columns directs save_text() to output the columns string at the top of the file. This effectively sets the headings for each column in the CSV file.
  • comments='' is necessary because save_text(), by default, prepends a # to the headers line. This options tells us that an empty string should be prepended instead.

The output file, array_x.csv, can be opened with any spreadsheet software.

The CSV file format is not standardized and each vendor uses slightly different conventions. More robust tools to create CSV files, as well as other data formats, are described in Chapter 4, Data Wrangling with pandas.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset