I have a python numpy ndarray with 2 numeric fields. Want to add a third field to it which is just the multiplication of the two. The two columns are named as "A" and "B" and I want the third column to be called "C". How should I proceed?
2 Answers
This is one way:
numpy.core.records.fromarrays([arr['A'], arr['B'], arr['A']*arr['B']], names='A,B,C')
numpy.lib.recfunctions.append_fields(arr, 'C', arr['A']*arr['B'])
Note that these will return a new array containing all the columns. There's no way to add a column in-place.