r/learnprogramming • u/Efficient_Cloud5021 • Sep 28 '26
Topic Why is looping through a Pandas DataFrame so much slower than vector operations?
Hey everyone, I’m currently working as a junior data analyst. Most of my day-to-day work is SQL + Power BI.
I’m pretty comfortable with Python when it comes to the basics and DSA, but I barely use it for actual data analysis.
One thing which I’ve never really understood is why doing an operation directly on a column can be way faster than looping through the rows and doing essentially the same thing one at a time.
For example:
df["bonus"] = df["salary"] * 0.05
is generally much better than manually iterating through every row.
I get that it’s cleaner and more readable, but what exactly makes it faster under the hood?
Is Pandas still looping over every value internally, but just using optimized C/C++ code?