IniziaInizia gratis

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

Questo esercizio fa parte del corso

Writing Efficient Code with pandas

Visualizza il corso

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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))
Modifica ed esegui il codice