Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy genfromtxt Column Names

Tags:

python

numpy

How can I have genfromtxt to return me its list of column names which were automatically retrieved by names=True? When I do:

data = np.genfromtxt("test.csv",names=True,delimiter=",",dtype=None)
print data['col1']

it prints the entire column values for col1.

However, I need to traverse all column names. How can I do that?

I tried data.keys() and various other methods, but whatever is returned by genfromtxt does not seem to be a dictionary compatible object. I guess I could pass the list of column names myself, but this won't be maintainable for me in the long run.

Any ideas?

like image 498
BBSysDyn Avatar asked May 30 '12 15:05

BBSysDyn


People also ask

How does Numpy Genfromtxt work?

genfromtxt. Load data from a text file, with missing values handled as specified. Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded.

Which argument should be passed into Genfromtxt If you have many column names to define from the data?

The only mandatory argument of genfromtxt is the source of the data. It can be a string, a list of strings, a generator or an open file-like object with a read method, for example, a file or io. StringIO object.

What is Numpy delimiter?

delimiter : The string used to separate values. By default, this is any whitespace. converters : A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: converters = {0: datestr2num}.


1 Answers

genfromtxtreturns a numpy.ndarray.

You can get the data type with

data.dtype

or just the names with

data.dtype.names

which is a tuple you can iterate over and access the columns as you want to.

like image 120
bmu Avatar answered Oct 14 '22 06:10

bmu