LoslegenKostenlos loslegen

Replace scalar values II

As discussed in the video, in a pandas DataFrame, it is possible to replace values in a very intuitive way: we locate the position (row and column) in the Dataframe and assign in the new value you want to replace with. In a more pandas-ian way, the .replace() function is available that performs the same task.

You will be using the names DataFrame which includes, among others, the most popular names in the US by year, gender and ethnicity.

Your task is to replace all the babies that are classified as FEMALE to GIRL using the following methods:

  • intuitive scalar replacement
  • using the .replace() function

Diese Übung ist Teil des Kurses

Writing Efficient Code with pandas

Kurs anzeigen

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

start_time = time.time()

# Replace all the entries that has 'FEMALE' as a gender with 'GIRL'
names['Gender'].____[____ == ____] = 'GIRL'

print("Time using .loc[]: {} sec".format(time.time() - start_time))
Code bearbeiten und ausführen