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())