Writing Efficient Code with pandas
Leonidas Souliotis
PhD Candidate
start_time = time.time()
names['Gender'].replace({'MALE':'BOY', 'FEMALE':'GIRL'},
inplace=True)
print("Time using .replace() with dictionary: {} sec".format(time.time() - start_time))
Time using .replace() with dictionary: 0.00197792053223 sec
start_time = time.time()
names['Gender'].replace('MALE', 'BOY', inplace=True)
names['Gender'].replace('FEMALE', 'GIRL', inplace=True)
print("Time using multiple .replace(): {} sec".format(time.time() - start_time))
Time using multiple .replace(): 0.00307083129883 sec
Difference in speed: 55.2555448407%
start_time = time.time()
names.replace({'Ethnicity': {'ASIAN AND PACI': 'ASIAN', 'ASIAN AND PACIFIC ISLANDER': 'ASIAN',
'BLACK NON HISPANIC': 'BLACK', 'BLACK NON HISP': 'BLACK',
'WHITE NON HISPANIC': 'WHITE', 'WHITE NON HISP': 'WHITE'}})
print("Time using .replace() with dictionary: {} sec".format (time.time() - start_time))
Time using .replace() with dictionary: 0.0028018 sec
Writing Efficient Code with pandas