IniziaInizia gratis

Replace single values II

For this exercise, we will be using the names DataFrame. In this dataset, the column 'Rank' shows the ranking of each name by year. For this exercise, you will use dictionaries to replace the first ranked name of every year as 'FIRST', the second name as 'SECOND' and the third name as 'THIRD'.

You will use dictionaries to replace one single value per key.

You can already see the first 5 names of the data, which correspond to the 5 most popular names for all the females belonging to the 'ASIAN AND PACIFIC ISLANDER' ethnicity in 2011.

Questo esercizio fa parte del corso

Writing Efficient Code with pandas

Visualizza il corso

Istruzioni dell'esercizio

  • Replace the ranks, indicated in numbers, by strings, following the pattern given above. Don't hesitate to explore your dataset in the Console after replacing values to see how it changed.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Replace the number rank by a string
names['Rank'].replace({____:____, ____:____, ____:____}, inplace=True)
print(names.head())
Modifica ed esegui il codice