Fix Python – How do I read CSV data into a record array in NumPy?

Question

Asked By – hatmatrix

Is there a direct way to import the contents of a CSV file into a record array, just like how R’s read.table(), read.delim(), and read.csv() import data into R dataframes?

Or should I use csv.reader() and then apply numpy.core.records.fromrecords()?

Now we will see solution for issue: How do I read CSV data into a record array in NumPy?


Answer

Use numpy.genfromtxt() by setting the delimiter kwarg to a comma:

from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')

This question is answered By – Andrew

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0