Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I rename fields in a numpy record array

I am new to python so this may sound very basic. I have imported a csv file using csv2rec. The first row has headers. I want to change the headers to 'x', 'y', 'z'. What's the best way of doing this?

>>> import matplotlib
>>> import matplotlib.mlab as mlab
>>> r= mlab.csv2rec('HeightWeight.csv', delimiter= ',')
>>> names= r.dtype.names
>>> for i in names:
     print i


index
heightinches
weightpounds
like image 839
user1995519 Avatar asked Jan 20 '13 22:01

user1995519


2 Answers

You can simply assign to .dtype.names:

>>> d = np.array([(1.0, 2), (3.0, 4)], dtype=[('a', float), ('b', int)])
>>> d
array([(1.0, 2), (3.0, 4)], 
      dtype=[('a', '<f8'), ('b', '<i8')])
>>> d['a']
array([ 1.,  3.])
>>> d.dtype.names
('a', 'b')
>>> d.dtype.names = 'x', 'y'
>>> d
array([(1.0, 2), (3.0, 4)], 
      dtype=[('x', '<f8'), ('y', '<i8')])
>>> d['x']
array([ 1.,  3.])

Same way with recarray:

>>> d
rec.array([(1.0, 2), (3.0, 4)], 
      dtype=[('a', '<f8'), ('b', '<i8')])
>>> d.dtype.names = 'apple', 'pear'
>>> d
rec.array([(1.0, 2), (3.0, 4)], 
      dtype=[('apple', '<f8'), ('pear', '<i8')])
like image 155
DSM Avatar answered Sep 20 '22 20:09

DSM


There is a rename_fields method in numpy.lib.recfunctions for that purpose. It also works with masked arrays.

import numpy as np
import numpy.lib.recfunctions as rfn

ab = np.ma.zeros(3, dtype=[('a', 'f4'), ('b', 'i4')])
xy = rfn.rename_fields(ab, {'a': 'x', 'b': 'y'})

print(ab.dtype, ab.mask.dtype)
print(xy.dtype, xy.mask.dtype)

Output:

[('a', '<f4'), ('b', '<i4')] [('a', '?'), ('b', '?')]
[('x', '<f4'), ('y', '<i4')] [('x', '?'), ('y', '?')]
like image 44
sfinkens Avatar answered Sep 20 '22 20:09

sfinkens