w3resource

NumPy String: numpy.char.isnumeric() function

numpy.char.isnumeric() function

For each element the numpy.char.isnumeric() function returns True if there are only numeric characters in the element.

This method is useful for tasks that require identifying elements that represent numeric values in a dataset, such as data cleaning and preprocessing tasks. It is also useful for validating user input in applications that require numeric input.

Syntax:

numpy.char.isnumeric(a)

Parameter:

Name Description Required /
Optional
a: array_like, unicode Input array. Required

Return value:

Array of booleans of same shape as a.

Example: Checking if string contains only numeric characters using numpy.char.isnumeric()

>>> import numpy as np
>>> x = np.char.isnumeric('APPLE')
>>> x
array(False)

In the above code the numpy.char.isnumeric() function checks whether the given string 'APPLE' contains only numeric characters. The function returns a boolean value of 'False' as the string contains alphabets and not numeric characters.

Pictorial Presentation:

NumPy String operation: isnumeric() function

Example: Checking if a string is numeric using numpy.char.isnumeric()

>>> import numpy as np
>>> y = np.char.isnumeric('12345')
>>> y
array(True)

In the above example, the variable 'y' is assigned the result of applying np.char.isnumeric() on the string '12345', which consists of numeric characters only. Therefore, the function returns True and the variable 'y' is assigned an array containing a single Boolean value True.

Pictorial Presentation:

NumPy String operation: isnumeric() function

Example: numpy.char.isnumeric() function

>>> import numpy as np
>>> z = np.char.isnumeric('12345APPLE')
>>> z
array(False)

In the above case, '12345APPLE' is a non-numeric string, so the output is False.

Pictorial Presentation:

NumPy String operation: isnumeric() function

Python - NumPy Code Editor:

Previous: islower()
Next: isspace()



Follow us on Facebook and Twitter for latest update.