Vectorizing .upper()
There are many situations where you might want to use Python methods and functions on array elements in NumPy. You can always write a for
loop to do this, but vectorized operations are much faster and more efficient, so consider using np.vectorize()
!
You've got an array called names
which contains first and last names:
names = np.array([["Izzy", "Monica", "Marvin"],
["Weber", "Patel", "Hernandez"]])
You'd like to use one of the Python methods you learned on DataCamp, .upper()
, to make all the letters of every name in the array uppercase. As a reminder, .upper()
is a string method, meaning that it must be called on an instance of a string: str.upper()
.
Your task is to vectorize this Python method. numpy
is loaded for you as np
, and the names
array is available.
This exercise is part of the course
Introduction to NumPy
Exercise instructions
- Create a vectorized function called
vectorized_upper
from the Python.upper()
string method. - Apply
vectorized_upper()
to thenames
array and save the resulting array asuppercase_names
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Vectorize the .upper() string method
vectorized_upper = ____
# Apply vectorized_upper to the names array
uppercase_names = ____
print(uppercase_names)