python - How do I add a column to a 2d array with specific dtype? -


here code wrote. 'guteliste25.txt' contains data table header names of colums specified.

import numpy np d = 'guteliste25.txt' cns = np.genfromtxt(d, dtype = none, names = true)  dt = np.dtype([('r','<f8')]) test = np.ones(len(cns),dtype=dt) klaus = np.concatenate((cns,test), axis=1) 

the error spits out in last line is: typeerror: expected readable buffer object

i think must problem np.genfromtxt , formatting of different rows.

i want add 1 value each row, i.e. in total 1 column , have name can access via: cns['r']

its best create structured array scratch , fill it:

the 2 examples begin with:

import numpy np numpy.lib import recfunctions  ints = np.ones(5,dtype=np.int) floats = np.ones(5,dtype=np.float) strings = np.array(['a']*5) 

to create empty structured array , fill values:

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')] 

to append data current array:

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 name applied first array [('f0', '>i4'), ('floats', '>f8'), ('strings', 's4')] 

i highly recommend python pandas dealing structured arrays.