What is the numpy.char.isnumeric() function in Python?

Overview

The char.isnumeric() function in Python returns `true` if there are only numeric characters in the element of a given input array. Otherwise, it returns false.

Pictorial description

Illustration of how char.isnumeric() function works

Syntax

char.isnumeric(a)

Parameters

The char.isnumeric() function takes an array as a parameter.

Return value

The char.isnumeric() function returns an array of boolean values with the same shape as the input array, a.

Example

import numpy as np
# creating an input array
a = np.array(["Hi", "25", "3L", "6C", "2"])
# invoking the char.isnumeric() function
myarray = np.char.isnumeric(a)
# printing the input array
print(a)
# printing the output arrays
print(myarray)

Explanation

  • Line 1: We import thenumpymodule.
  • Line 3: We create an input array a using the array() function of numpy.
  • Line 6: We invoke the char.isnumeric() function on the input array. The result is assigned to a variable, myarray.
  • Line 9: We print the input array, a.
  • Line 12: We print myarray.

Free Resources