Following is the simplified version of my problem. I want to create a (N, 1)
shape numpy array, which would have strings as their values. However, when I try to insert the string, only the first character of the string gets inserted.
What am I doing wrong here?
>>> import numpy as np
>>> N = 23000
>>> Y = np.empty((N, 1), dtype=str)
>>> Y.shape
(23000, 1)
>>> for i in range(N):
... Y[i] = "random string"
...
>>> Y[10]
array(['r'], dtype='<U1')
By default data type str
takes length of 1
. So, you will only get one character. we can set max data length by using np.dtype('U100')
. Un
where U
is unicode and n
is number of characters in it.
Try below code
>>> import numpy as np
>>> N = 23000
>>> Y = np.empty((N, 1), dtype=np.dtype('U100'))
>>> Y.shape
(23000, 1)
>>> for i in range(N):
... Y[i] = "random string"
...
>>> Y[10]
array(['random string'], dtype='<U100')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With