使用 .replace() 取代標量值

使用 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
使用 pandas 撰寫高效程式碼

在 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
使用 pandas 撰寫高效程式碼

使用 .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%
使用 pandas 撰寫高效程式碼

開始動手做

使用 pandas 撰寫高效程式碼

Preparing Video For Download...