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
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')])
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', '?')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With