genfromtxt

Using numpy.genfromtxt to read a csv file with strings containing commas

六月ゝ 毕业季﹏ 提交于 2019-11-26 22:29:33
I am trying to read in a csv file with numpy.genfromtxt but some of the fields are strings which contain commas. The strings are in quotes, but numpy is not recognizing the quotes as defining a single string. For example, with the data in 't.csv': 2012, "Louisville KY", 3.5 2011, "Lexington, KY", 4.0 the code np.genfromtxt('t.csv', delimiter=',') produces the error: ValueError: Some errors were detected ! Line #2 (got 4 columns instead of 3) The data structure I am looking for is: array([['2012', 'Louisville KY', '3.5'], ['2011', 'Lexington, KY', '4.0']], dtype='|S13') Looking over the

Using genfromtxt to import csv data with missing values in numpy

自古美人都是妖i 提交于 2019-11-26 14:25:36
问题 I have a csv file that looks something like this (actual file has many more columns and rows): 1,2,3,4,5 6,7,8,9,10 11,12,13,14,15 16 Say the name of the file is info.csv If I try to import this using data = numpy.genfromtxt('info.csv', delimiter = ',') then I get the following error: ValueError: Some errors were detected ! Line #4 (got 1 columns instead of 5) If I use, data = numpy.genfromtxt('info.csv', delimiter = ',', skip_footer = 1) both lines with data 16 and with data 11, 12, 13, 14,

numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why?

家住魔仙堡 提交于 2019-11-26 14:19:05
问题 I'm running genfromtxt like below: date_conv = lambda x: str(x).replace(":", "/") time_conv = lambda x: str(x) a = np.genfromtxt(input.txt, delimiter=',', skip_header=4, usecols=[0, 1] + radii_indices, converters={0: date_conv, 1: time_conv}) Where input.txt is from this gist. When I look at the results, it is a 1D array not a 2D array: >>> np.shape(a) (918,) It seems to be an array of tuples instead: >>> a[0] ('06/03/2006', '08:27:23', 6.4e-05, 0.000336, 0.001168, 0.002716, 0.004274, 0

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

二次信任 提交于 2019-11-25 22:43:00
问题 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() ? 回答1: 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