pandas로 효율적인 코드 작성하기
Leonidas Souliotis
PhD Candidate
| 출생연도 | 성별 | 인종 | 아이 이름 | 수 | 순위 |
|---|---|---|---|---|---|
| 2011 | FEMALE | WHITE NON HISP | HELENA | 97 | 4 |
start_time = time.time()
names['Ethnicity'].loc[(names["Ethnicity"] == 'WHITE NON HISPANIC') |
(names["Ethnicity"] == 'WHITE NON HISP')] = 'WNH'
print("Results from the above operation calculated in %s seconds" %
(time.time() - start_time))
위 방법의 결과 계산 시간: 0.0276169776917초
start_time = time.time()
names['Ethnicity'].replace(['WHITE NON HISPANIC','WHITE NON HISP'],
'WNH', inplace=True)
print("Time using .replace(): {} sec".format(time.time() - start_time))
.replace() 사용 시간: 0.00144791603088초
속도 차이: 2160.68681809%
names['Ethnicity'].replace(['WHITE NON HISP'], 'WHITE NON HISPANIC', inplace=True)
names['Ethnicity'].replace(['BLACK NON HISP'], 'BLACK NON HISPANIC', inplace=True)
names['Ethnicity'].replace(['BLACK NON HISP','WHITE NON HISP'], ['BLACK NON HISPANIC',
'WHITE NON HISPANIC'], inplace=True)
pandas로 효율적인 코드 작성하기