MulaiMulai sekarang secara 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

Latihan ini adalah bagian dari kursus

Writing Efficient Code with pandas

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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))
Edit dan Jalankan Kode