실전으로 보는 pandas 벡터화
이 연습 문제에서는 pandas 시리즈에 벡터화를 적용하여 다음을 수행합니다:
- 각 패(행)에서 모든 카드 랭크의 평균을 계산합니다.
- 각 패에서 5장 카드(열) 각각의 랭크 평균을 계산합니다.
두 방법의 효율성을 비교하기 위해 poker_hands 데이터셋을 다시 사용합니다.
이 연습은 강의의 일부입니다
pandas로 효율적인 코드 작성하기
연습 안내
- 각 패의 평균 랭크를 계산하세요.
- 모든 패에 대해 5장 카드 각각의 평균 랭크를 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Calculate the mean rank in each hand
row_start_time = time.time()
mean_r = poker_hands[['R1', 'R2', 'R3', 'R4', 'R5']].____(axis=____)
print("Time using pandas vectorization for rows: {} sec".format(time.time() - row_start_time))
print(mean_r.head())
# Calculate the mean rank of each of the 5 card in all hands
col_start_time = time.time()
mean_c = poker_hands[['R1', 'R2', 'R3', ____, ____]].____(____=____)
print("Time using pandas vectorization for columns: {} sec".format(time.time() - col_start_time))
print(mean_c.head())