Signup/Sign In

NumPy replace() function

In this tutorial, we will cover numpy.char.replace() function of the char module in Numpy library.

The replace() function is used to return a copy of the array of strings or the string, with all occurrences of the old substring replaced by the new substring. This function is very useful if you want to do some changes in the array elements, where you want to replace a substring with some new string value.

We can also specify the count, to specify how many occurences of the old substring must be replaced. For example if we have a string homesweethome, and we want to replace only the first home substring with something else, then we can specify the count as 1. If we specify the count as 2, then both home substring will be replaced with the new substring.

Note: If we do not specify the count parameter, then also, by default all the occurrences of the ol substring will be replaced by the new one.

This function calls the str.replace for every array element.

Syntax of replace():

The syntax required to use this function is as follows:

numpy.char.replace(a, old, new, count=None)

Let's cover the parameters of this function.

Parameters:

let us discuss the above-given parameters of this function:

  • a
    This parameter is used to indicate an array of strings.

  • old
    This parameter indicates the old substring that you want to replace.

  • new
    This parameter indicates the new substring that is used to replace the old substring.

  • count
    This is an optional argument. We have already explained about this in the introduction of this page.

Returned Values:

This function will return the output array of strings.

Example 1:

The code snippet is as follows where we will use replace() function:

import numpy as np

string1="It is a yellow chair"
print("The original string is:\n",string1)

x = np.char.replace(string1, 'It', 'This')
print("After applying replace() function:")
print(x)


The original string is:
It is a yellow chair
After applying replace() function:
This is a yellow chair

Example 2: Using the count param

In this example, we will also pass the count parameter to the replace() function:

import numpy as np

string1="This is a chair.The color of the chair is yellow"
print("The original string is:\n",string1)

x = np.char.replace(string1, 'chair', 'book', count=1)
print("After applying replace() function:")
print(x)

As in the above code, the string contains the substring "chair" twice but at the time of using the replace() function we have passed count=1, hence only the first occurence of the substring chair should be replaced.


The original string is:
This is a chair. The color of the chair is yellow
After applying replace() function:
This is a book. The color of the chair is yellow

Due to the count=1, this function replaces only the first appearance of the chair and not second.

Use of replace() function:

Let's take an example to cover an basic usecase, to help you understand where we can use this function. For example, if the data in an ndarray contains price of any product in form of string values, like $10,000, $5,000, and so on. Here the price values are not number, instead they are strings with $ sign and a comma(,) which will make it difficult for us to perform any mathematical operation on these values.

So we can use the replace() function to remove the $ sign from each value and replace it with nothing(provide empty substring as new parameter). Similarly for the comma(,) too, and then we will get numeric strings which we can convert to integer while performing any mathematical operation.

Summary

In this tutorial we learned about replace() function of the Numpy library which is very useful for handling array of strings, in case you want to replace any particular part of the string.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.