Numpy Isnumeric Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will cover the isnumeric() function of the char module in the Numpy library. This function is used to check if a string contains only numeric characters. The function returns True if there are only numeric characters in the string, otherwise, it returns False.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/booleans -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/lists -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/tuples -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/importing_modules -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/standard_libraries -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/numerical_computing -.-> lab-86462{{"`Numpy Isnumeric Function`"}} python/build_in_functions -.-> lab-86462{{"`Numpy Isnumeric Function`"}} numpy/1d_array -.-> lab-86462{{"`Numpy Isnumeric Function`"}} numpy/data_array -.-> lab-86462{{"`Numpy Isnumeric Function`"}} numpy/bool_idx -.-> lab-86462{{"`Numpy Isnumeric Function`"}} numpy/fancy_idx -.-> lab-86462{{"`Numpy Isnumeric Function`"}} end

Import Numpy Library

We need to import the numpy library before we can use the isnumeric() function. We use the import keyword followed by the library name numpy and the nickname np:

import numpy as np

Using isnumeric() with a Single String

We can use the isnumeric() function to check if a single string contains only numeric characters. Let's use an example string "12Apple90" and apply the isnumeric() function to it:

import numpy as np

string1 = "12Apple90"
print("The Input string is:")
print(string1)

x = np.char.isnumeric(string1)
print("The Output is:")
print(x)

Output:

The Input string is:
12Apple90
The Output is:
False

As we can see, the isnumeric() function returns False as there are non-numeric characters in the input string.

Using isnumeric() with an Array

We can also use the isnumeric() function with an array of strings. Let's use an example array inp_ar which contains a mixture of numeric and non-numeric strings:

import numpy as np

inp_ar = np.array(['1', '2000', '90', '3.5', '0'])
print("The Input array is:")
print(inp_ar)

outp_arr = np.char.isnumeric(inp_ar)
print("The Output array is:")
print(outp_arr)

Output:

The Input array is:
['1' '2000' '90' '3.5' '0']
The Output array is:
[ True  True  True False  True]

As we can see, the isnumeric() function returns an array of boolean values with True indicating that the string contains only numeric characters and False indicating that the string contains non-numeric characters.

Limitations of isnumeric()

It is important to note that the isnumeric() function returns False for a string with a numeric value with a decimal, as shown in Example 2 above.

Summary

In this lab, we learned about the isnumeric() function of the Numpy library. We covered how to use it with single strings and arrays, as well as the limitations of the function.

Other Python Tutorials you may like