How to create a numpy record array?

Make each row a tuple, not a list:

import numpy as np
x = np.array([(1, 'O', 1)],
             dtype=np.dtype([('step', 'int32'),
                             ('symbol', '|S1'),
                             ('index', 'int32')]))

Numpy developer Robert Kern explains:

As a rule, tuples are considered "scalar" records and lists are recursed upon. This rule helps numpy.array() figure out which sequences are records and which are other sequences to be recursed upon; i.e. which sequences create another dimension and which are the atomic elements.


I will show a more general way of creating record array:

# prepare the array with different types
recarr = np.zeros((4,), dtype=('i4,f4,a10'))

# creating the columns
col1 = [1, 7, 2, 3]
col2 = [1.1, 0.5, 2, 7.45]
col3 = ['This', 'is', 'text', '!!!']

# create a list of tuples from columns
# prepare = zip(col1, col2, col3)  # Python 2

prepare = list(zip(col1, col2, col3))  # Python 3

# assigning value so recarr
recarr[:] = prepare

Now you can assign names for each of the columns:

recarr.dtype.names = ('ID' , 'price', 'text')

and later get the values for this column:

print recarr('price')

Tags:

Python

Numpy