How can I determine if a Numpy array contains a string? The array a
in
a = np.array('hi world')
has data type dtype('|S8')
, where 8
refers to the number of characters in the string.
I don't see how regular expressions (such as re.match('\|S\d+', a.dtype)
) would work here as the data type isn't simply '|S8'
.
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.
Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the “in” operator. “in” operator is used to check whether certain element and values are present in a given sequence and hence return Boolean values 'True” and “False“.
The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects.
a.dtype.char == 'S'
or
a.dtype.type is np.string_
See NumPy docs, Data type objects, Attributes.
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