Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Return a copy of the string with all occurrences of substring old replaced by new in Numpy
To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy −
- The 1st parameter is the input array
- The 2nd parameter is the old string to be replaced
- The 3rd parameter is the new string to be replaced with the old
If the optional parameter count is given, only the first count occurrences are replaced
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.
Steps
At first, import the required library −
import numpy as np
Create an array −
arr = np.array(["Welcome to the Jungle", "Jungle Safari"])
Displaying our array −
print("Array...<br>",arr)
Get the datatype −
print("\nArray datatype...<br>",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...<br>",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...<br>",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...<br>",arr.size)
To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy −
print("\nResult...<br>",np.char.replace(arr, 'Jungle', 'Club'))
Example
import numpy as np
# Create an array
arr = np.array(["Welcome to the Jungle", "Jungle Safari"])
# Displaying our Array
print("Array...<br>",arr)
# Get the datatype
print("\nArray datatype...<br>",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...<br>",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...<br>",arr.shape)
# Get the number of elements of the Array
print("\nElements in the Array...<br>",arr.size)
# To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy
# The 1st parameter is the input array
# The 2nd parameter is the old string to be replaced
# The 3rd parameter is the new string to be replaced with the old
print("\nResult...<br>",np.char.replace(arr, 'Jungle', 'Club'))
Output
Array... ['Welcome to the Jungle' 'Jungle Safari'] Array datatype... <U21 Array Dimensions... 1 Our Array Shape... (2,) Elements in the Array... 2 Result... ['Welcome to the Club' 'Club Safari']
Advertisements
