使用 .values() 将 NumPy 向量化应用于数组

使用 pandas 编写高效代码

Leonidas Souliotis

PhD candidate

pandas 中的 NumPy

df = pd.DataFrame({'Col1':[0, 1, 
2, 3, 4, 5, 6]}, dtype=np.int8)
print(df)
|        | Col1 | 
|--------|------| 
| 0      | 0    | 
| 1      | 1    | 
| 2      | 2    | 
| 3      | 3    | 
| 4      | 4    | 
| 5      | 5    | 
| 6      | 6    |
nd = np.array(range(7))
print(nd)

[0 1 2 3 4 5 6]
使用 pandas 编写高效代码

如何进行 NumPy 向量化

start_time = time.time()
poker[['R1', 'R2', 'R3', 'R4', 'R5']].values.sum(axis=1)
print("Time using NumPy vectorization: {} sec(time.time() - start_time))
上述操作使用 0.00157618522644 秒计算完成
start_time = time.time()
poker[['R1', 'R2', 'R3', 'R4', 'R5']].sum(axis=1)
print("Results from the above operation calculated in %s seconds" % (time.time() - start_time))
上述操作使用 0.00268197059631 秒计算完成
时间差:39.0482%
使用 pandas 编写高效代码

开始练习!

使用 pandas 编写高效代码

Preparing Video For Download...