Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get recarray attributes/columns python

I'm trying to retrieve the column titles of a recarray, and running into considerable trouble. If I read in a .csv file using pylab's csv2rec function, I am able to access column titles in the following manner:

from pylab import csv2rec
x = csv2rec(file.csv)
x.column1
x.column2

Where 'column1' is the title of the first column, and it would return the rest of the values in the column. But, I am reading in a .csv file where I don't know what all of the values of the column titles are, and I want to be able to access them (either loop through, or set a list). This seems like it should be simple. Any ideas?

like image 621
mike Avatar asked Feb 02 '23 10:02

mike


1 Answers

You can use x.dtype.names:

>>> import numpy as np

>>> a = np.array([0.1,0.2])
>>> b = np.array([0.3,0.4])
>>> dtype = {'names' : ['a','b'], 'formats' : ['f8', 'f8']}
>>> c = np.rec.fromarrays([a,b], dtype = dtype)
>>> c
rec.array([(0.1, 0.3), (0.2, 0.4)], 
      dtype=[('a', '<f8'), ('b', '<f8')])
>>> print c.dtype.names
('a', 'b')

Or, using your example:

[physics@aurora ~/calc ]$ cat csv.dat 
a,b
0.1,0.3
0.2,0.4

In [1]: from pylab import csv2rec

In [2]: x = csv2rec('csv.dat')

In [3]: for name in x.dtype.names:
   ...:         print name
a
b
like image 193
Andrey Sobolev Avatar answered Feb 05 '23 02:02

Andrey Sobolev