Skip to main content
  1. Posts/

I Reduced My Pandas Runtime by 95%: Here's What I Was Doing Wrong

··280 words·2 mins·

🐼 Your Pandas Code “Works”… But Is It Efficient?

Many developers confuse “code without errors” with “efficient code.” With Pandas, you can get the correct result and still be doing things in the worst way possible.

⚡ The most costly mistakes slowing down Pandas:

  • 🐌 Using .apply() with lambda: iterates row by row, like a for loop. It’s up to 6,000× slower than the alternative.
  • 📝 The right operation: vectorization — operating on entire columns at once using native Pandas/NumPy operations.
# Slow 🐌
df.apply(lambda row: row['sales'] * row['discount'], axis=1)

# Fast ⚡
df['sales'] * df['discount']
  • 🏋️ Bloated data types: storing integers as int64 when int32 is enough doubles memory usage and slows everything down.
  • 🔍 Not measuring before optimizing: use %timeit to compare approaches before rewriting code.

🎯 The key is understanding when Pandas reaches its limits and considering alternatives like Polars or Dask for massive datasets.

💡 Explanation in a nutshell
#

Pandas is like a very powerful spreadsheet for Python. When you ask it to process data row by row (with .apply()), it’s slow because it makes an individual trip for each row. Vectorization, on the other hand, is like telling it “process all rows at once” — the internal NumPy engine is optimized to do it in a single operation, much faster. It’s the difference between making 100,000 short trips versus one long trip.

More information at the link 👇

Also published on LinkedIn.
Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano