Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
NumPy provides a collection of functions to perform vectorized string operations for arrays of dtype numpy.string_
or numpy.unicode_
. They are primarily based on Python's string methods.
Let's dive into a tutorial on string operations provided by NumPy:
When working with textual data in arrays, you often need to perform operations like splitting, joining, or replacing substrings. NumPy facilitates these operations using its vectorized string functions.
Start by importing the necessary library:
import numpy as np
numpy.char.add()
:This function performs element-wise string concatenation.
arr1 = np.array(['Hello', 'Hi']) arr2 = np.array([' World', ' there']) print(np.char.add(arr1, arr2)) # Output: ['Hello World' 'Hi there']
numpy.char.multiply()
:Replicates each string element.
arr = np.array(['Hello ', 'Bye ']) print(np.char.multiply(arr, 3)) # Output: ['Hello Hello Hello ' 'Bye Bye Bye ']
numpy.char.center()
:Centers a string and fills the remaining space with the specified fill character.
arr = np.array(['Hello', 'Bye']) print(np.char.center(arr, 10, fillchar='*')) # Output: ['**Hello***' '***Bye****']
numpy.char.capitalize()
:Capitalizes the first letter of the string.
arr = np.array(['hello', 'world']) print(np.char.capitalize(arr)) # Output: ['Hello' 'World']
numpy.char.split()
:Splits each string element.
arr = np.array(['Hello World', 'Hi there']) print(np.char.split(arr)) # Output: [list(['Hello', 'World']) list(['Hi', 'there'])]
numpy.char.replace()
:Replace substrings.
arr = np.array(['He is a boy', 'She is a girl']) print(np.char.replace(arr, 'is', 'was')) # Output: ['He was a boy' 'She was a girl']
numpy.char.strip()
:Removes leading and/or trailing characters.
arr = np.array(['..Hello..', '..World..']) print(np.char.strip(arr, '.')) # Output: ['Hello' 'World']
numpy.char.join()
:Joins string elements.
separator = ':' arr = np.array(['Hello', 'World']) print(np.char.join(separator, arr)) # Output: ['H:e:l:l:o' 'W:o:r:l:d']
numpy.char.lower()
and numpy.char.upper()
:Converts strings to lower and upper cases respectively.
arr = np.array(['Hello', 'World']) print(np.char.lower(arr)) # Output: ['hello' 'world'] print(np.char.upper(arr)) # Output: ['HELLO' 'WORLD']
NumPy's string functions provide a powerful set of tools for performing vectorized string operations on arrays. While they might not replace the extensive capabilities of Python's native string methods or libraries like Pandas for text-heavy workflows, they come in handy for quick, element-wise text manipulations in NumPy arrays.
NumPy provides functions for handling strings efficiently within arrays.
import numpy as np # Create a string array string_array = np.array(["apple", "banana", "cherry", "date"]) # Access individual string elements first_char = string_array[0][0] substring = string_array[1][2:5] print("First Character:", first_char) print("Substring:", substring)
Perform string handling within NumPy arrays for efficient data manipulation.
# Assuming 'string_array' is already defined # Access individual string elements first_char = string_array[0][0] substring = string_array[1][2:5] print("First Character:", first_char) print("Substring:", substring)
NumPy provides various string operations for efficient manipulation within arrays.
# Assuming 'string_array' is already defined # Perform string operations uppercase_strings = np.char.upper(string_array) concatenated_strings = np.char.add(string_array, " fruit") print("Uppercase Strings:") print(uppercase_strings) print("\nConcatenated Strings:") print(concatenated_strings)
Utilize NumPy's numpy.char
functions for efficient string handling within arrays.
# Assuming 'string_array' is already defined # Use numpy.char functions uppercase_strings = np.char.upper(string_array) concatenated_strings = np.char.add(string_array, " fruit") print("Uppercase Strings:") print(uppercase_strings) print("\nConcatenated Strings:") print(concatenated_strings)
Code samples showcasing various string manipulations within NumPy arrays.
import numpy as np # Create a string array string_array = np.array(["apple", "banana", "cherry", "date"]) # Access individual string elements first_char = string_array[0][0] substring = string_array[1][2:5] # Perform string operations uppercase_strings = np.char.upper(string_array) concatenated_strings = np.char.add(string_array, " fruit") print("First Character:", first_char) print("Substring:", substring) print("\nUppercase Strings:") print(uppercase_strings) print("\nConcatenated Strings:") print(concatenated_strings)
Combine and split strings efficiently within NumPy arrays.
# Assuming 'string_array' is already defined # Concatenate strings concatenated_strings = np.char.add(string_array, " fruit") # Split strings split_strings = np.char.split(concatenated_strings) print("Concatenated Strings:") print(concatenated_strings) print("\nSplit Strings:") print(split_strings)
Perform string comparison and sorting operations within NumPy arrays.
# Assuming 'string_array' is already defined # String comparison is_apple = np.char.equal(string_array, "apple") # Sort strings sorted_strings = np.sort(string_array) print("Is 'apple' in the array:", is_apple) print("\nSorted Strings:") print(sorted_strings)
Format strings efficiently using NumPy's string formatting capabilities.
# Assuming 'string_array' is already defined # Format strings formatted_strings = np.char.mod("%s is a fruit", string_array) print("Formatted Strings:") print(formatted_strings)