How to export a multidimensional NumPy array to a CSV file?

Mary-Kate Olsen
Release: 2024-11-12 17:50:02
Original
551 people have browsed it

How to export a multidimensional NumPy array to a CSV file?

Dumping a NumPy Array into a CSV File

Exporting multidimensional NumPy arrays to CSV files is a common requirement for data manipulation and analysis. This question explores the use of NumPy's savetxt function to achieve this task.

Solution

NumPy provides the savetxt function, which offers a convenient method for writing arrays to a text file in the comma-separated values (CSV) format.

import numpy

# Create a 2D NumPy array
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

# Save array to CSV file
numpy.savetxt("foo.csv", a, delimiter=",")
Copy after login

Explanation

  • The savetxt function takes two main arguments: the filename and the array to save.
  • The delimiter, which separates individual array elements in the output CSV file, can be specified. In this case, a comma is used.
  • The resulting "foo.csv" file will contain the array data in a human-readable format suitable for import into other software packages or analysis tools.

The above is the detailed content of How to export a multidimensional NumPy array to a CSV file?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template