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]])


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...