How do I read CSV data into a record array in NumPy?
Posted By: Anonymous
I wonder if there is a direct way to import the contents of a CSV file into a record array, much in the way that R’s read.table()
, read.delim()
, and read.csv()
family imports data to R’s data frame?
Or is the best way to use csv.reader() and then apply something like numpy.core.records.fromrecords()
?
Solution
You can use Numpy’s genfromtxt()
method to do so, by setting the delimiter
kwarg to a comma.
from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')
More information on the function can be found at its respective documentation.
Answered By: Anonymous
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.