使用 pandas 编写高效代码
Leonidas Souliotis
PhD Candidate
| 出生年份 | 性别 | 族裔 | 名字 | 计数 | 排名 |
|---|---|---|---|---|---|
| 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))
第一种方法耗时 0.0311849 秒
start_time = time.time()
names['Gender'].replace('MALE', 'BOY', inplace=True)
print("Time using .replace(): {} sec".fomrat(time.time() - start_time))
使用 .replace() 的时间:0.0016758441925 秒
速度差异:1,704.52411439%
使用 pandas 编写高效代码