create structured numpy array in python with strings and int
create structured numpy array in python with strings and int
i have this:
>>> matriz
[['b8:27:eb:d6:e3:10', '0.428s', '198'],
['b8:27:eb:d6:e3:10', '0.428s', '232'],
['b8:27:eb:07:65:ad', '0.796s', '180'],
['b8:27:eb:07:65:ad', '0.796s', '255'],
dtype='<U17']`
but i need the column
`matriz[:, [2]] :
[['198'],
['232'],
['180'],
['255']]`
to be int and the other columns to be strings, i was trying with structured numpy array but i have this error message,
ValueError: invalid literal for int() with base 10: 'b8:27:eb:d6:e3:10'
TypeError: a bytes-like object is required, not 'str'
i used
matriz=np.array(matriz, dtype='U17,U17,i4')
i'm using numpy version '1.12.1' for raspberry pi 3, i don't know what i'm doing wrong.
thanks a lot
Also why do you want to do this, if you want mixed datatypes you should be using
pandas
– user3483203
Jun 30 at 23:23
pandas
Thanks you for the correction, yes, i need to mix those data because i want to know what data from column matriz[:, [2]] are for column matriz[:, [0]]
– Santiago molina sanchez
Jun 30 at 23:35
do you know if i can do that with pandas?
– Santiago molina sanchez
Jun 30 at 23:38
2 Answers
2
In [484]: x = np.array([['b8:27:eb:d6:e3:10', '0.428s', '198'],
...: ['b8:27:eb:d6:e3:10', '0.428s', '232'],
...: ['b8:27:eb:07:65:ad', '0.796s', '180'],
...: ['b8:27:eb:07:65:ad', '0.796s', '255']],
...: dtype='<U17')
...:
You could fetch the last column with an astype
conversion:
astype
In [485]: x[:,2].astype(int)
Out[485]: array([198, 232, 180, 255])
In [486]: x[:,[2]].astype(int)
Out[486]:
array([[198],
[232],
[180],
[255]])
To construct a structured array, you need to provide a list of tuples. A list of lists or non-structured array with the compound dtype will produce your kind of error.
In [487]: np.array([tuple(i) for i in x],'U17,U10,int')
Out[487]:
array([('b8:27:eb:d6:e3:10', '0.428s', 198),
('b8:27:eb:d6:e3:10', '0.428s', 232),
('b8:27:eb:07:65:ad', '0.796s', 180),
('b8:27:eb:07:65:ad', '0.796s', 255)],
dtype=[('f0', '<U17'), ('f1', '<U10'), ('f2', '<i8')])
In [488]: _['f2']
Out[488]: array([198, 232, 180, 255])
Fields of the structured array are fetched by name.
NumPy works best with homogeneous dtype arrays. Pandas is a good alternative if you have different types.
However, what you ask is possible with NumPy structured arrays:
import numpy as np
x = np.array([['b8:27:eb:d6:e3:10', '0.428s', '198'],
['b8:27:eb:d6:e3:10', '0.428s', '232'],
['b8:27:eb:07:65:ad', '0.796s', '180'],
['b8:27:eb:07:65:ad', '0.796s', '255']],
dtype='<U17')
arr = np.core.records.fromarrays(x.transpose(),
formats='<U17,<U17,i4',
names='col1,col2,col3')
print(arr)
rec.array([('b8:27:eb:d6:e3:10', '0.428s', 198),
('b8:27:eb:d6:e3:10', '0.428s', 232),
('b8:27:eb:07:65:ad', '0.796s', 180),
('b8:27:eb:07:65:ad', '0.796s', 255)],
dtype=[('col1', '<U17'), ('col2', '<U17'), ('col3', '<i4')])
thanks you a lot, i need to mix those data because i want to know what data from column arr[:, [2]] are for column arr[:, [0]]. Do you know if i can do that from this way? thanks you...
– Santiago molina sanchez
Jul 1 at 0:01
arr['col3']
is how you access a field of the structured array.– hpaulj
Jul 1 at 0:30
arr['col3']
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You really should post your data as text, not as images
– user3483203
Jun 30 at 23:20