Vectorization vs. For-Loop
1. The Python For-Loop
for i in range(5):
rng[i] = tmax[i] - tmin[i]
| Idx | tmax | tmin | temp_range |
| 0 | 19.6 | 11.1 | |
| 1 | 21.8 | 12.3 | |
| 2 | 24.8 | 12.7 | |
| 3 | 26.2 | 14.0 | |
| 4 | 28.1 | 15.2 | |
Time: 0.00s
2. Pandas Vectorization
# Math applied instantly to entire array
rng = data["tmax"] - data["tmin"]
| Idx | tmax | tmin | temp_range |
| 0 | 19.6 | 11.1 | |
| 1 | 21.8 | 12.3 | |
| 2 | 24.8 | 12.7 | |
| 3 | 26.2 | 14.0 | |
| 4 | 28.1 | 15.2 | |
Time: 0.00s
Click the buttons above to compare how Python loops vs. Pandas vectorization process data.