genfromtxt

Reading data into numpy array from text file

有些话、适合烂在心里 提交于 2019-12-17 16:12:07
问题 I have a file with some metadata, and then some actual data consisting of 2 columns with headings. Do I need to separate the two types of data before using genfromtxt in numpy? Or can I somehow split the data maybe? What about placing the file pointer to the end of the line just above the headers, and then trying genfromtxt from there? Thanks The format of the file is shown below: &SRS <MetaDataAtStart> multiple=True Wavelength (Angstrom)=0.97587 mode=assessment background=True issid=py11n2g

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

无人久伴 提交于 2019-12-17 06:08:13
问题 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([[

How do I load heterogeneous data (np.genfromtxt) as a 2D array?

耗尽温柔 提交于 2019-12-13 19:43:52
问题 I learn from numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why? that numpy.genfromtxt returns a structured ndarray if the data is not homogeneous. How do I load heterogeneous data as a 2D array? For instance, a text file whose contents are: (all items except the header are int ) # c1 c2 c3 c4 c5 3 4 8 6 8 10 7 6 7 10 5 10 2 1 3 7 6 5 3 6 5 8 5 2 7 1 2 2 10 8 10 5 9 3 8 5 2 4 4 2 Load data using np.genfromtxt , # load data from a text file table = np.genfromtxt(

numpy genfromtxt converters unknown number of columns

陌路散爱 提交于 2019-12-13 14:22:50
问题 I have several data numeric files in which the decimal separator is a comma. So I use a lambda function to do a conversion: import numpy as np def decimal_converter(num_cols): conv = dict((col, lambda valstr: \ float(valstr.decode('utf-8').replace(',', '.'))) for col in range(nb_cols)) return conv data = np.genfromtxt("file.csv", converters = decimal_converter(3)) the data in the file is like this: 0; 0,28321815; 0,5819178 1; 0,56868281; 0,85621369 2; 0,24022026; 0,53490058 3; 0,63641921; 0

Is it possible to add a new field in a numpy.genfromtxt output?

纵然是瞬间 提交于 2019-12-13 07:12:51
问题 I loaded a csv file and used the header to specify the names of each column. # Load the Data data = np.genfromtxt('dat_h.csv', delimiter=',', names=True) This is great, because I can access the columns by their name. For example... DATES = data['Dates'] Temperature = data['Temp'] Say I have a vector of pressure observations that matches these measurements. Can I append the data structure with a new field that includes my pressure variable? I want to do something like this ... data.append(

genfromtxt and numpy

北慕城南 提交于 2019-12-13 07:09:36
问题 I have data in files such as "file.csv". I would like to read them with np.genfromtxt and do some statistics like average, variance etc. on some columns (X, Y, Z) . However I want to make the statistics on for X > 1, Y > 3 Z > 2 etc. This is a simple example here. This code produces almost correct results but it includes ALL Xs, Ys and Zs, I want to do the same but with the X,Y,Z conditions i specified above. #file.csv X,Y,Z 1,2,3 4,2,5 15,9,1 # data = np.genfromtxt(file.csv, delimiter=',',

numpy genfromtxt/pandas read_csv; ignore commas within quote marks

狂风中的少年 提交于 2019-12-12 11:40:28
问题 Consider a file, a.dat , with contents: address 1, address 2, address 3, num1, num2, num3 address 1, address 2, address 3, 1.0, 2.0, 3 address 1, address 2, "address 3, address4", 1.0, 2.0, 3 I am trying to import with numpy.genfromtxt. However the function sees an additional column in row 3. I get a similar error with pandas.read_csv: np.genfromtxt('a.dat',delimiter=',',dtype=None,skiprows=1) ValueError: Some errors were detected ! Line #3 (got 7 columns instead of 6) and pandas read_csv

Skip a specified number of columns with numpy.genfromtxt() python 3.4 error

柔情痞子 提交于 2019-12-11 12:37:38
问题 import os import numpy as np import matplotlib.pyplot as plt # Open a file path = "input/" filelist = list(filter(lambda s: s.endswith(".asc"), os.listdir(path))) firstImage = np.genfromtxt (" ".join(ln.split()[1:]) for ln in path+next(iter(filelist))) what is wrong? getting: TypeError: Can't convert 'bytes' object to str implicitly 回答1: Check out the function's doc, it seems able to to all kinds of crazy things out of the box : http://docs.scipy.org/doc/numpy/reference/generated/numpy

Python: numpy.genfromtxt - Need column names that contain invalid characters

妖精的绣舞 提交于 2019-12-11 07:57:26
问题 I am working on importing CSV files with numpy.genfromtxt . The data to be imported has a header of column names, and some of those column names contain characters that genfromtxt considers invalid. Specifically, some of the names contain "#" and " ". The input data cannot be changed as it is generated by other sources that I do not control. Using names=True and comments=None , I am unable to bring in all of the column names that I need. I've tried overriding numpy.lib.NameValidator

numPy gives nan while reading a negative number from a file

ぐ巨炮叔叔 提交于 2019-12-11 07:05:15
问题 I tried to read the contents of a 3x3 matrix stored in a text file having the following values. −30 10 20 10 40 −50 20 −50 −10 I used numpy.genfromtxt as follows but when it gave nan in place of negative data values. data = np.genfromtxt('file.txt',delimiter=' ') print(data) print(type(data[0][0])) But the datatype of the negative value still shows float64 : I also tried reading the values as a list and then converting to numpy array but that also didn't work. Is there any other way I can