Numpy Index Function

PythonPythonBeginner
Practice Now

Introduction

This lab covers the index() function of the char module in the Numpy library. The main purpose of this function is to perform a string search operation on a given array of strings. If we have an array of strings then this function will provide the first index of any substring to be searched, if it is present in the array elements.

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/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) 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/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") 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/lists -.-> lab-86450{{"`Numpy Index Function`"}} python/tuples -.-> lab-86450{{"`Numpy Index Function`"}} python/importing_modules -.-> lab-86450{{"`Numpy Index Function`"}} python/catching_exceptions -.-> lab-86450{{"`Numpy Index Function`"}} python/numerical_computing -.-> lab-86450{{"`Numpy Index Function`"}} python/build_in_functions -.-> lab-86450{{"`Numpy Index Function`"}} numpy/1d_array -.-> lab-86450{{"`Numpy Index Function`"}} numpy/data_array -.-> lab-86450{{"`Numpy Index Function`"}} numpy/bool_idx -.-> lab-86450{{"`Numpy Index Function`"}} numpy/fancy_idx -.-> lab-86450{{"`Numpy Index Function`"}} end

Import Required Libraries

In the first step, we will import necessary libraries including the Numpy library.

import numpy as np

Create Input Array

In the second step, we will create an input array of strings.

ar = np.array(['bBaBaBb', 'baAbaB', 'abBABba'])

In the third step, we will use the index() function to search for a substring within the input array. We can specify the substring we want to search for. In this example, we are searching for the letter 'b' in the above array.

output = np.char.index(ar, sub ='b')

View Output

In the fourth step, we will view the output array to see the index values for the substring 'b' in the input array.

print ("The Output array:\n", output)

Handle Value Error

In the fifth step, we will try to search for a substring that does not exist in the input strings and handle the ValueError exception.

try:
    output = np.char.index(ar, sub ='c')
    print("The Output array:\n", output)
except ValueError as ve:
    print("ValueError: substring not found")

Summary

In this lab, we have covered the index() function of the Numpy library. We used this function to perform a string search operation on a given array of strings, provided the first index of any substring to be searched, if it is present in the array elements and handle the ValueError exception in case the substring wasn't found.

Other Python Tutorials you may like