I want to create an array with 3 columns. The first one a string, the other two integers used for calculations. Then more rows will be added through the append function (below).
When collecting values iteratively, it is usually best to collect them in a list, and make the array afterwards:
For example, making a list with your data:
In [371]: alist = [("String", 1, 2)]
In [372]: alist.append(("another string", 3, 4))
In [373]: alist
Out[373]: [('String', 1, 2), ('another string', 3, 4)]
For many purposes that list is quite useful, alist[0]
, or [i[0] for i in alist]
.
To make a list, one option is a structured array. Because I collected values as a list of tuples I can do:
In [374]: np.array(alist, dtype='U20,int,int')
Out[374]:
array([('String', 1, 2), ('another string', 3, 4)],
dtype=[('f0', '
We access fields
of such an array by field name. The array itself is 1d, (2,).
If instead we make an object
dtype array:
In [376]: np.array(alist, dtype=object)
Out[376]:
array([['String', 1, 2],
['another string', 3, 4]], dtype=object)
In [377]: _.shape
Out[377]: (2, 3)
In [378]: __[:,1]
Out[378]: array([1, 3], dtype=object)
With this we can access rows and columns. But beware that we don't get the fast numpy calculation benefits with a object array, especially one with mixed types.