pythonnumpyscipygenfromtxt

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


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()?


Solution

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

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