2017年3月5日 星期日

[ Python 常見問題 ] How to read csv into record array in numpy?

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:
  1. from numpy import genfromtxt  
  2. my_data = genfromtxt('my_file.csv', delimiter=',')  
I would recommend the read_csv function from the pandas library. Consider we have a csv file:
- test.csv
  1. 123  
  2. 456  
  3. 789  
  4. # Comment  
Then you can load it this way:
>>> import pandas as pd
// header : int or list of ints, default ‘infer’ - Row number(s) to use as the column names, and the start of the data. Default behavior is as if set to 0 if no names passed, otherwise None.
// comment : str, default None - Indicates remainder of line should not be parsed.
>>> df = pd.read_csv('test.csv', sep=',', header=None, comment='#')
>>> df.values
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])


沒有留言:

張貼留言

[ Py DS ] Ch5 - Machine Learning (Part2)

Source From  Here   Introducing Scikit-Learn   There are several Python libraries that provide solid implementations of a range of machin...