pandas 向量化實戰
在這個練習中,你會在 pandas Series 上套用向量化來:
- 計算每一手牌(列)中所有牌的平均點數階級
- 計算每一手牌中 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())