import numpy as np
d=np.dtype([('name',np.str_),('salary',np.int64)])
arr = np.array([('Vedant',80000),('Subodh',4000)],dtype=d)
print(arr)
Output:
[('', 80000) ('', 4000)]
Why are the strings blank?
esqew
45k28 gold badges139 silver badges180 bronze badges
1 Answer 1
You need to set how many chars you want to insert. ('name',np.str_, 4) this expect each word has only four chars. Or use ('name','U10') for considering ten chars, ('name','U18') for considering 18 chars without specifying.
import numpy as np
d=np.dtype([('name',np.str_, 4),('salary',np.int64)])
# we can define dtype like below without specifying the length of words
# BUT we should pay attention 'U10' only considers ten chars
# OR 'U18' for considering 18 chars
# d=np.dtype([('name','U10'),('salary',np.int64)])
arr = np.array([('abcde',80000),('Subodh',4000)],dtype=d)
print(arr)
# [('abcd', 80000) ('Subo', 4000)]
# skip 'e' from 'abcde'
# skip 'dh' from 'Subodh'
answered Jul 25, 2022 at 10:51
Mahdi F.
24.1k5 gold badges25 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
hpaulj
I would use the
('name', 'U10') style. The shape for the float is an unnecessary complication here.lang-py