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
Return element-wise string concatenation for two arrays of string in Numpy
To return element-wise string concatenation for two arrays of string, use the numpy.char.add() method in Python Numpy.
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.
The function add() returns the output array of string_ or unicode_, depending on input types of the same shape as x1 and x2. The x1 and x1 are input arrays.
Steps
At first, import the required library −
import numpy as np
Create two One-Dimensional arrays of string
arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad']) arr2 = np.array(['Cio', 'Hanks', 'Ceo', 'Hudson', 'Adams', 'Pitt'])
Display the arrays −
print("Array 1...<br>", arr1)
print("\nArray 2...<br>", arr2)
Get the type of the arrays −
print("\nOur Array 1 type...<br>", arr1.dtype)
print("\nOur Array 2 type...<br>", arr2.dtype)
Get the dimensions of the Arrays −
print("\nOur Array 1 Dimensions...<br>",arr1.ndim)
print("\nOur Array 2 Dimensions...<br>",arr2.ndim)
Get the shape of the Arrays −
print("\nOur Array 1 Shape...<br>",arr1.shape)
print("\nOur Array 2 Shape...<br>",arr2.shape)
To return element-wise string concatenation for two arrays of string, use the numpy.char.add() method. The arr1 and arr2 are the two input string arrays −
print("\nResult...<br>",np.char.add(arr1,arr2))
Example
import numpy as np
# Create two One-Dimensional arrays of string
arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad'])
arr2 = np.array(['Cio', 'Hanks', 'Ceo', 'Hudson', 'Adams', 'Pitt'])
# Display the arrays
print("Array 1...<br>", arr1)
print("\nArray 2...<br>", arr2)
# Get the type of the arrays
print("\nOur Array 1 type...<br>", arr1.dtype)
print("\nOur Array 2 type...<br>", arr2.dtype)
# Get the dimensions of the Arrays
print("\nOur Array 1 Dimensions...<br>",arr1.ndim)
print("\nOur Array 2 Dimensions...<br>",arr2.ndim)
# Get the shape of the Arrays
print("\nOur Array 1 Shape...<br>",arr1.shape)
print("\nOur Array 2 Shape...<br>",arr2.shape)
# To return element-wise string concatenation for two arrays of string, use the numpy.char.add() method in Python Numpy
# The arr1 and arr2 are the two input string arrays
print("\nResult...<br>",np.char.add(arr1,arr2))
Output
Array 1... ['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad'] Array 2... ['Cio' 'Hanks' 'Ceo' 'Hudson' 'Adams' 'Pitt'] Our Array 1 type... <U5 Our Array 2 type... <U6 Our Array 1 Dimensions... 1 Our Array 2 Dimensions... 1 Our Array 1 Shape... (6,) Our Array 2 Shape... (6,) Result... ['BellaCio' 'TomHanks' 'JohnCeo' 'KateHudson' 'AmyAdams' 'BradPitt']
