Adding a field to a structured numpy array (2)

走远了吗. 提交于 2019-11-27 15:30:17

As far as documentation for the recfunctions, here it is: http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html

You might also want to read this conversation: http://comments.gmane.org/gmane.comp.python.numeric.general/39537

If you can't figure it out after reading those, then please provide some code samples (for instance, did you import numpy.lib.recfunctions?) and an error message or undesired result, so we can determine how to fix it.

http://projects.scipy.org/numpy/browser/branches/1.3.x/numpy/lib/recfunctions.py?rev=8229

did you import?

from numpy.lib import recfunctions

recfunctions.append_field(*your_args)

Seems like everything is working:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.3.0'
>>> from numpy.lib.recfunctions import append_fields
>>> append_fields
<function append_fields at 0x9e3e80c>
>>> 

Here is a concrete example how to use append_fields(..) (admittedly based on the other answers here):

import numpy as np
x = np.array(np.arange(0,10), dtype = [('x', float)])
y = np.array(np.arange(10,20), dtype = [('y', float)])

from numpy.lib.recfunctions import append_fields

z = append_fields(x, 'y', y)

where

z.dtype.names

will give

('x', 'y')

note that y in can also be 'plain' ndarray without column names:

y = np.arange(10,20)

or you can rename the column y to something else (even if y is a structured array with column names):

z = append_fields(x, 'p', y)

(tested in numpy 1.6.1)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!