Writing Efficient Code with pandas
Leonidas Souliotis
PhD Candidate
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 |
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
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%
Writing Efficient Code with pandas