Source From Here
Question
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()?
How-To
You can use Numpy's genfromtxt() method to do so, by setting the delimiter kwarg to a comma:
I would recommend the
read_csv function from the pandas library. Consider we have a csv file:
- test.csv
Then you can load it this way:
Question
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()?
How-To
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=',')
- test.csv
- 1, 2, 3
- 4, 5, 6
- 7, 8, 9
- # Comment
沒有留言:
張貼留言