1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to NumPy

Connected

Exercise

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.

Instructions

100 XP
  • Create a vectorized function called vectorized_upper from the Python .upper() string method.
  • Apply vectorized_upper() to the names array and save the resulting array as uppercase_names.