Optimal pandas iterating

Writing Efficient Python Code

Logan Thomas

Scientific Software Technical Trainer, Enthought

pandas internals

  • Eliminating loops applies to using pandas as well
  • pandas is built on NumPy
    • Take advantage of NumPy array efficiencies

alt=”pandas library logo on top of NumPy package logo on top of python programming language logo; line drawn from pandas to Numpy with text Builds on and line drawn from NumPy to python with text Builds on”

Writing Efficient Python Code
print(baseball_df)
  Team League  Year   RS   RA   W    G  Playoffs
0  ARI     NL  2012  734  688  81  162         0
1  ATL     NL  2012  700  600  94  162         1
2  BAL     AL  2012  712  705  93  162         1
...
wins_np = baseball_df['W'].values

print(type(wins_np))
<class 'numpy.ndarray'>
print(wins_np)
[ 81  94  93 ...]
Writing Efficient Python Code

Power of vectorization

  • Broadcasting (vectorizing) is extremely efficient!
baseball_df['RS'].values - baseball_df['RA'].values
array([  46,  100,    7, ...,  188,  110, -117])
Writing Efficient Python Code

Run differentials with arrays

run_diffs_np = baseball_df['RS'].values - baseball_df['RA'].values

baseball_df['RD'] = run_diffs_np print(baseball_df)
     Team League  Year   RS   RA    W    G  Playoffs   RD
0     ARI     NL  2012  734  688   81  162         0   46
1     ATL     NL  2012  700  600   94  162         1  100
2     BAL     AL  2012  712  705   93  162         1    7
3     BOS     AL  2012  734  806   69  162         0  -72
4     CHC     NL  2012  613  759   61  162         0 -146
...
Writing Efficient Python Code

Comparing approaches

%%timeit
run_diffs_np = baseball_df['RS'].values - baseball_df['RA'].values

baseball_df['RD'] = run_diffs_np
124 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Writing Efficient Python Code

Let's put our skills into practice!

Writing Efficient Python Code

Preparing Video For Download...