| by Arround The Web | No comments

Numpy Replace Method

NumPy is the built-in library provided by a python that enables us to create multi-dimensional arrays and manipulate them and perform various calculations on them. When it comes to updating the data in arrays, a question may arise about how we will be changing them. The predefined method is to replace the values or the entire row or column within the array is used, the replace() method enables us to update the substring value within the string or character arrays. As Python strings are not changeable, we are not able to change the value yet we can use the replace() method to generate a new string that retains the substrings that were updated.

Syntax

The syntax to use replace method is as below:

numpy.char.replace(array, old-substring, new-substring, count = None)

The Following Arguments are Passed to it

array: It is the given array in which we tend to change or replace substring or any value.

Old-substring: It is the substring within the array that is to be replaced.

New-substring: It is the one to which the old one is replaced.

Count: count is not required but in some cases if we have to replace more than once we add a count, which replaces the number of times mentioned.

For example: numpy.char.replace(arr, old-substring, new-substring, count=3)

As mentioned above, the count value is “3” which means it will replace the old substring with the new one that occurs at the first 3 places.

Example # 01:

Now, we have performed an example here to understand its functionality in depth to explain the concept of replace method. In this example, we are going to perform the below code. We have initialized a variable str_a and another variable replace_a. We have passed a sentence to our str_a which is a character string-based variable. We have called np.char.replace because we are performing our replacement operation on a character. That is why we have called np.char.replace.

After that, we passed our variable str_a to the function and passed the value to be replaced and the replaced value in the second and third arguments. We are going to replace the value ‘numpy’ with ‘The Numpy’. The function will search the value ‘numpy’ in the statement and replace it with the value ‘The Numpy’ value wherever it is placed.

import numpy as np

str_a="numpy is a library for the Python programming language"

print("original string:\n",str_a)

replace_a = np.char.replace(str_a, 'numpy', 'Numpy')

print("resulting string after replacing:")

print(replace_a)

After executing our code our output will be the following. We can see that the function has replaced ‘numpy’ with ‘The Numpy’ value. One thing to keep in mind is that wherever the input value will occur, the system will replace it with our passed parameter. In our case, it has occurred only once so the system has replaced it only one time.

Example # 02:

NumPy.replace function not only takes static values as arguments but we can also pass variables as our values to it as arguments. An illustration of such a method is shown. In the example below, we have declared a variable x and passed it an array value in character format. We will print its value just to compare it with the replaced value.

After that, we declared a variable old_value and passed it a character string. This is the value we will replace in our character array x. We have initialized another variable and passed it on to some characters. This value will replace the values passed in the old_value variable. So, by looking at the code, we can understand that we are trying to replace ‘are you’ with ‘is the boy’.

But in this case, we are not passing these values directly but we are storing these values in variables and we will try to replace those variables with the help of our function. We have declared a variable final_value which will store the output result from our code snippet. After that, we performed our numPy.char.replace function and stored the output in our final_value variable. Let us execute our code and check our output.

import numpy as np

x = np.array(["Hi, what are you doing?"])

print("Old value:", end = " ")

print(x)

old_value = "are you"

new_value = "is the boy"

final_value = np.char.replace(x, old_value, new_value)

print('New value:', end = " ")

print(final_value)

After executing the code, the following output will be given by our compiler. We can see that in our old value it is ‘what are you doing’ but in the new value, the function has replaced the ‘are you’ characters with ‘is the boy’ characters. But we did not pass these values to our function directly but passed them using variables. So, by performing this activity, we can easily replace the values using variables in the NumPy.replace function.

Example # 03:

We discussed earlier that we can replace multiple characters also using the numpy.replace function. In this illustration we have tried to do so, we have initialized a variable ‘a’ and assigned it a character array with repeating words just to check whether all those words will be replaced or a single one and if a single word is replaced which one will it be. We have passed the word ‘again’ more than one time in this case.

Then, we have declared two more variables ‘old_val’ and ‘rep_val’ which will hold the value to be replaced and the value after the replace function respectively. After that, we initialized the last variable ‘fin_val’ which will store the result from the function. Now, we will perform our function. We have passed ‘a’ as our input character array, ‘org_val’ as the original or old value parameter, and ‘rep_val’ as the replaced value or new value argument. We will execute it and print the variable to check the output.

import numpy as np

a = np.array(["we will replace again and again and again"])

print(a)

org_value = "again"

rep_value = "once"

fin_value = np.char.replace(a, org_value, rep_value)

print(fin_value)

After the execution, we will get the following output. We can see that the word ‘again’ was repeated 3 times in our character array. The function has replaced it with ‘once’ on all positions. This explains that the replace function also works on multiple word replacements at the same time.

Conclusion

In this guide, we learned about the numpy.char.replace function of python’s NumPy. We explained what the purpose of the replace function is. We also learned its syntax and the arguments we have to pass to our function to work. NumPy.char.replace is a very useful method and comes in handy when we are performing operations on our character arrays or variables. If we want to change a word or correct a word in large character arrays. We will not have to change those words one by one manually. Instead, we can use our NumPy.replace function to do the job. We also performed examples to make you understand the idea more thoroughly. NumPy.replace is a very effective and useful function and can be applied in several circumstances.

Share Button

Source: linuxhint.com

Leave a Reply