Расширение ответа @ unutbu Я публикую более общую функцию, которая добавляет любое количество строк:
def append_rows(arrayIN, NewRows):
"""Append rows to numpy recarray.
Arguments:
arrayIN: a numpy recarray that should be expanded
NewRows: list of tuples with the same shape as `arrayIN`
Idea: Resize recarray in-place if possible.
(only for small arrays reasonable)
>>> arrayIN = np.array([(1, 'a', 1.1), (2, 'dd', 2.0), (3, 'x', 3.0)],
dtype=[('a', '<i4'), ('b', '|S3'), ('c', '<f8')])
>>> NewRows = [(4, '12', 4.0), (5, 'cc', 43.0)]
>>> append_rows(arrayIN, NewRows)
>>> print(arrayIN)
[(1, 'a', 1.1) (2, 'dd', 2.0) (3, 'x', 3.0) (4, '12', 4.0) (5, 'cc', 43.0)]
Source: http://stackoverflow.com/a/1731228/2062965
"""
# Calculate the number of old and new rows
len_arrayIN = arrayIN.shape[0]
len_NewRows = len(NewRows)
# Resize the old recarray
arrayIN.resize(len_arrayIN + len_NewRows, refcheck=False)
# Write to the end of recarray
arrayIN[-len_NewRows:] = NewRows
Комментарий
Хочу подчеркнуть, что предварительное выделение массива, по крайней мере достаточно большого, является наиболее разумным решением (если у вас есть представление об окончательном размере массива)! Предварительное распределение также экономит много времени.