python 2.7 - Two-Dimensional structured array -


i trying construct structured array in python can accessed names of columns , rows. possible structured array method of numpy?

example: array should have form:

my_array =        b c                  e 1 2 3                  f 4 5 6                  g 7 8 9  

and want have possibility following:

my_array["a"]["e"] = 1 my_array["c"]["f"] = 6 

is possible in pyhton using structured arrays or there type of structure more suitable such task?

with recarray, can access columns dot notation or specific reference column name. rows, accessed row number. haven't seen them accessed via row name, example:

>>> import numpy np >>> = np.arange(1,10,1).reshape(3,3) >>> dt = np.dtype([('a','int'),('b','int'),('c','int')]) >>> a.dtype = dt >>> r = a.view(type=np.recarray) >>> r rec.array([[(1, 2, 3)],        [(4, 5, 6)],        [(7, 8, 9)]],        dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<i4')]) >>> r.a array([[1],        [4],        [7]]) >>> r['a'] array([[1],        [4],        [7]]) >>> r.a[0] array([1]) >>> a['a'][0] array([1]) >>> # row >>> >>> r[0] rec.array([(1, 2, 3)],        dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<i4')]) >>> 

you can specify dtype , type @ same time

>>> = np.ones((3,3)) >>> b = a.view(dtype= [('a','<f8'), ('b','<f8'),('c', '<f8')], type = np.recarray) >>> b rec.array([[(1.0, 1.0, 1.0)],        [(1.0, 1.0, 1.0)],        [(1.0, 1.0, 1.0)]],        dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')]) >>> b.a array([[ 1.],        [ 1.],        [ 1.]]) >>> b.a[0] array([ 1.]) 

Comments