最好从头开始创建结构化数组并填充它:
这两个例子开始于:
import numpy as np
from numpy.lib import recfunctions
ints = np.ones(5,dtype=np.int)
floats = np.ones(5,dtype=np.float)
strings = np.array(['A']*5)
要创建一个空的结构化数组,然后用值填充它:
out=np.empty(5,dtype=[('ints', '>i4'), ('floats', '>f8'), ('strings', '|S4')])
out['ints']=ints
out['floats']=floats
out['strings']=strings
print out
[(1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A')]
print out.dtype
[('ints', '>i4'), ('floats', '>f8'), ('strings', 'S4')]
要将数据附加到当前数组:
out=np.lib.recfunctions.append_fields(ints,['floats','strings'],[floats,strings])
print out
[(1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A') (1, 1.0, 'A')]
print out.dtype #note the name applied to the first array
[('f0', '>i4'), ('floats', '>f8'), ('strings', 'S4')]
我强烈推荐使用python pandas来处理结构化数组。