Înlocuirea valorilor scalare cu .replace()

Scrierea codului eficient cu pandas

Leonidas Souliotis

PhD Candidate

Setul de date cu nume populare

Year of Birth Gender Ethnicity Child's First Name Count Rank
2011 FEMALE ASIAN AND PACIFIC ISLANDER SOPHIA 119 1
2011 FEMALE ASIAN AND PACIFIC ISLANDER CHLOE 106 2
Scrierea codului eficient cu pandas

Înlocuirea valorilor în pandas

start_time = time.time()
names['Gender'].loc[names.Gender=='MALE'] = 'BOY'
print("Replace values using .loc[]: {} sec".format(time.time() - start_time))
Results from the first method calculated in 0.0311849 seconds
Scrierea codului eficient cu pandas

Înlocuirea valorilor cu .replace()

start_time = time.time()
names['Gender'].replace('MALE', 'BOY', inplace=True)
print("Time using .replace(): {} sec".fomrat(time.time() - start_time))
Time using .replace(): 0.0016758441925 sec
Differerence in speed: 1,704.52411439%
Scrierea codului eficient cu pandas

Să exersăm

Scrierea codului eficient cu pandas

Preparing Video For Download...