LoslegenKostenlos loslegen

Replace multiple values I

In this exercise, you will apply the .replace() function for the task of replacing multiple values with one or more values. You will again use the names dataset which contains, among others, the most popular names in the US by year, gender and Ethnicity.

Thus you want to replace all ethnicities classified as black or white non-hispanics to non-hispanic. Remember, the ethnicities are stated in the dataset as follows: ['BLACK NON HISP', 'BLACK NON HISPANIC', 'WHITE NON HISP' , 'WHITE NON HISPANIC'] and should be replaced to 'NON HISPANIC'

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 non-Hispanic ethnicities with 'NON HISPANIC'
names['Ethnicity'].____[(names["Ethnicity"] == ____) | 
                      (____["Ethnicity"] == ____) | 
                      (____[____] == ____) | 
                      (____[____] == ____)] = ____

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