![Revisit consent button](../images/body-df29beacd7496220cc94.svg)
We value your privacy
We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies.
Customise Reject All Accept All
Customise Consent Preferences ![](../images/body-2159bb67f3de8ad870eb.svg)
We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.
The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more
NecessaryAlways Active
Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.
  * Cookie
BCTempID
  * Duration
10 minutes
  * Description
No description available.


  * Cookie
__cf_bm
  * Duration
1 hour
  * Description
This cookie, set by Cloudflare, is used to support Cloudflare Bot Management. 


  * Cookie
AWSALBCORS
  * Duration
7 days
  * Description
Amazon Web Services set this cookie for load balancing.


  * Cookie
_cfuvid
  * Duration
session
  * Description
Cloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services


  * Cookie
li_gc
  * Duration
6 months
  * Description
Linkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.


  * Cookie
__hssrc
  * Duration
session
  * Description
This cookie is set by Hubspot whenever it changes the session cookie. The __hssrc cookie set to 1 indicates that the user has restarted the browser, and if the cookie does not exist, it is assumed to be a new session.


  * Cookie
__hssc
  * Duration
1 hour
  * Description
HubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.


  * Cookie
wpEmojiSettingsSupports
  * Duration
session
  * Description
WordPress sets this cookie when a user interacts with emojis on a WordPress site. It helps determine if the user's browser can display emojis properly.


  * Cookie
BCSessionID
  * Duration
1 year 1 month 4 days
  * Description
Blueconic sets this cookie as a unique identifier for the BlueConic profile.


  * Cookie
_octo
  * Duration
1 year
  * Description
No description available.


  * Cookie
logged_in
  * Duration
1 year
  * Description
No description available.


  * Cookie
__Secure-YEC
  * Duration
past
  * Description
YouTube sets this cookie to stores the user's video player preferences using embedded YouTube video


  * Cookie
__eoi
  * Duration
6 months
  * Description
Description is currently not available.


  * Cookie
AWSALBTGCORS
  * Duration
7 days
  * Description
No description available.


  * Cookie
login-status-p
  * Duration
past
  * Description
Description is currently not available.


  * Cookie
AWSALBTG
  * Duration
7 days
  * Description
No description available.


  * Cookie
csrf_token
  * Duration
session
  * Description
No description available.


  * Cookie
token_v2
  * Duration
1 day
  * Description
Description is currently not available.


  * Cookie
D
  * Duration
1 year
  * Description
Description is currently not available.


  * Cookie
PHPSESSID
  * Duration
session
  * Description
This cookie is native to PHP applications. The cookie stores and identifies a user's unique session ID to manage user sessions on the website. The cookie is a session cookie and will be deleted when all the browser windows are closed.


  * Cookie
VISITOR_PRIVACY_METADATA
  * Duration
6 months
  * Description
YouTube sets this cookie to store the user's cookie consent state for the current domain. 


  * Cookie
cookietest
  * Duration
session
  * Description
The cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.


  * Cookie
__Host-airtable-session
  * Duration
1 year
  * Description
This cookie is used to enable us to integrate the services of Airtable.


  * Cookie
__Host-airtable-session.sig
  * Duration
1 year
  * Description
This cookie is used to enable us to integrate the services of Airtable.


  * Cookie
m
  * Duration
1 year 1 month 4 days
  * Description
Stripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.


  * Cookie
BIGipServer*
  * Duration
session
  * Description
Marketo sets this cookie to collect information about the user's online activity and build a profile about their interests to provide advertisements relevant to the user. 


  * Cookie
__cfruid
  * Duration
session
  * Description
Cloudflare sets this cookie to identify trusted web traffic.


  * Cookie
_GRECAPTCHA
  * Duration
6 months
  * Description
Google Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.


  * Cookie
__Secure-YNID
  * Duration
6 months
  * Description
Google cookie used to protect user security and prevent fraud, especially during the login process.


  * Cookie
cookieyes-consent
  * Duration
1 year
  * Description
CookieYes sets this cookie to remember users' consent preferences so that their preferences are respected on subsequent visits to this site. It does not collect or store any personal information about the site visitors.


Functional
Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.
  * Cookie
lidc
  * Duration
1 day
  * Description
LinkedIn sets the lidc cookie to facilitate data center selection.


  * Cookie
brw
  * Duration
1 year
  * Description
No description available.


  * Cookie
brwConsent
  * Duration
5 minutes
  * Description
Description is currently not available.


  * Cookie
WMF-Uniq
  * Duration
1 year
  * Description
Description is currently not available.


  * Cookie
loom_anon_comment
  * Duration
1 year
  * Description
No description available.


  * Cookie
loom_referral_video
  * Duration
session
  * Description
Description is currently not available.


  * Cookie
VISITOR_INFO1_LIVE
  * Duration
6 months
  * Description
A cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.


  * Cookie
yt-remote-connected-devices
  * Duration
Never Expires
  * Description
YouTube sets this cookie to store the user's video preferences using embedded YouTube videos.


  * Cookie
ytidb::LAST_RESULT_ENTRY_KEY
  * Duration
Never Expires
  * Description
The cookie ytidb::LAST_RESULT_ENTRY_KEY is used by YouTube to store the last search result entry that was clicked by the user. This information is used to improve the user experience by providing more relevant search results in the future.


  * Cookie
yt-remote-device-id
  * Duration
Never Expires
  * Description
YouTube sets this cookie to store the user's video preferences using embedded YouTube videos.


  * Cookie
yt-remote-session-name
  * Duration
session
  * Description
The yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.


  * Cookie
yt-remote-fast-check-period
  * Duration
session
  * Description
The yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.


  * Cookie
yt-remote-session-app
  * Duration
session
  * Description
The yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.


  * Cookie
yt-remote-cast-available
  * Duration
session
  * Description
The yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.


  * Cookie
yt-remote-cast-installed
  * Duration
session
  * Description
The yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.


  * Cookie
cp_session
  * Duration
3 months
  * Description
Codepen sets this cookie for Help systems found in the website.


  * Cookie
loid
  * Duration
1 year 1 month 4 days
  * Description
This cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.


Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.
  * Cookie
__hstc
  * Duration
6 months
  * Description
Hubspot set this main cookie for tracking visitors. It contains the domain, initial timestamp (first visit), last timestamp (last visit), current timestamp (this visit), and session number (increments for each subsequent session).


  * Cookie
hubspotutk
  * Duration
6 months
  * Description
HubSpot sets this cookie to keep track of the visitors to the website. This cookie is passed to HubSpot on form submission and used when deduplicating contacts.


  * Cookie
_ga
  * Duration
1 year 1 month 4 days
  * Description
Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors.


  * Cookie
_ga_*
  * Duration
1 year 1 month 4 days
  * Description
Google Analytics sets this cookie to store and count page views.


  * Cookie
__Host-psifi.analyticsTrace
  * Duration
6 hours
  * Description
Description is currently not available.


  * Cookie
__Host-psifi.analyticsTraceV2
  * Duration
6 hours
  * Description
Description is currently not available.


  * Cookie
_gh_sess
  * Duration
session
  * Description
GitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.


  * Cookie
YSC
  * Duration
session
  * Description
YSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.


  * Cookie
ajs_anonymous_id
  * Duration
1 year
  * Description
This cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.


  * Cookie
vuid
  * Duration
1 year 1 month 4 days
  * Description
Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. 


Performance
Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.
  * Cookie
AWSALB
  * Duration
7 days
  * Description
AWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.


  * Cookie
acq
  * Duration
past
  * Description
Description is currently not available.


  * Cookie
acq.sig
  * Duration
past
  * Description
Description is currently not available.


  * Cookie
ptc
  * Duration
2 years
  * Description
No description available.


Advertisement
Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.
  * Cookie
muc_ads
  * Duration
1 year 1 month 4 days
  * Description
Twitter sets this cookie to collect user behaviour and interaction data to optimize the website.


  * Cookie
guest_id_marketing
  * Duration
1 year 1 month 4 days
  * Description
Twitter sets this cookie to identify and track the website visitor.


  * Cookie
guest_id_ads
  * Duration
1 year 1 month 4 days
  * Description
Twitter sets this cookie to identify and track the website visitor.


  * Cookie
personalization_id
  * Duration
1 year 1 month 4 days
  * Description
Twitter sets this cookie to integrate and share features for social media and also store information about how the user uses the website, for tracking and targeting.


  * Cookie
guest_id
  * Duration
1 year 1 month 4 days
  * Description
Twitter sets this cookie to identify and track the website visitor. It registers if a user is signed in to the Twitter platform and collects information about ad preferences.


  * Cookie
bcookie
  * Duration
1 year
  * Description
LinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.


  * Cookie
__Secure-ROLLOUT_TOKEN
  * Duration
6 months
  * Description
YouTube sets this cookie to manage feature rollout and experimentation. It helps Google control which new features or interface changes are shown to users as part of testing and staged rollouts, ensuring consistent experience for a given user during an experiment.


  * Cookie
yt.innertube::nextId
  * Duration
Never Expires
  * Description
YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.


  * Cookie
yt.innertube::requests
  * Duration
Never Expires
  * Description
YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.


  * Cookie
session_tracker
  * Duration
session
  * Description
This cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.


  * Cookie
edgebucket
  * Duration
session
  * Description
Reddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.


  * Cookie
did
  * Duration
1 year
  * Description
Arbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.


Uncategorised
Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet.
No cookies to display.
Reject All Save My Preferences Accept All
Toggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation
[](https://towardsdatascience.com/)
Publish AI, ML & data-science insights to a global community of data professionals.
Sign in[Submit an Article](https://write.insightmediagroup.io)
[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)
* * *
[Write For TDS](https://towardsdatascience.com/submissions/)
[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search
[Programming](https://towardsdatascience.com/category/programming/)
# I Reduced My Pandas Runtime by 95% — Here’s What I Was Doing Wrong
Most slow Pandas code "works", until it doesn't. Learn how to spot hidden bottlenecks, avoid costly row-wise operations, and know when Pandas is no longer enough.
[Ibrahim Salami](https://towardsdatascience.com/author/ibrahim.salami/)
April 26, 202617 min read
![](../images/000.jpg)
Created with Nano Banana
I've been learning Pandas for some time now. Nothing too crazy though. Just basic data cleaning, exploratory data analysis, and some essential functions. I've also explored things like method chaining for cleaner, more organized code, and operations that silently break your Pandas workflow, both of which I've written about before.
I never really thought about runtime. Honestly, if my code ran without errors and gave me the output I needed, I was happy. Even if it took a few minutes for all my notebook cells to finish, I didn't care. No errors meant no problems, right?
Then I came across the concept of vectorization. And something clicked.
I went down the rabbit hole, as I usually do. The more I read, the more I realized that "no errors" and "efficient code" are two very different things. Your Pandas code can be completely correct and still be quietly terrible at scale.
So this article is me documenting what I found. The mistakes that slow Pandas code down, why they happen, how to fix them, and when Pandas itself might be the bottleneck. If you've ever run a notebook and just assumed the wait time was normal, this one's for you.
Learn this step by step with the interactive [Computer Science](https://roadmap.sh/computer-science) roadmap.
## Why "Working Code" Isn't Good Enough
There's a reason this took me a while to think about. Pandas is designed to be forgiving. You can write code in a dozen different ways and most of them will work. You get your output, your dataframe looks right, and you move on.
But that flexibility comes with a hidden cost.
Unlike SQL or production-grade data systems, Pandas doesn't force you to think about efficiency. It doesn't warn you when you're doing something expensive. It just... does it. Slowly, sometimes. But it does it.
Think about it this way. SQL has a query optimizer. It looks at what you're asking for and figures out the most efficient way to get it. Pandas doesn't have that. It trusts you to write efficient code. And if you don't know what efficient looks like, you'll never know you're missing it.
The result is that a lot of Pandas code in the wild is what I'd call politely inefficient. It works on small datasets. It works on medium datasets with a little patience. But the moment you throw real-world data at it, something that's a few hundred thousand rows or more, the cracks start to show. What used to take seconds now takes minutes. What took minutes becomes unusable.
And the frustrating part is nothing looks wrong. No errors. No warnings. Just a slow notebook and a spinning cursor.
That's the trap. Pandas optimizes for convenience, not speed. And convenience is great, until it isn't.
So the first shift is a mindset one: working code and efficient code are not the same thing. Once that clicks, everything else follows.
## Profiling: Stop Guessing, Start Measuring
Here's something I noticed while going down this rabbit hole. Most people, when they feel like their code is slow, do one of two things. They either rewrite the whole thing from scratch hoping something improves, or they just accept it and wait.
Neither of those is the right move.
The right move is to measure first. You can't optimize what you haven't identified. And more often than not, the part of your code you _think_ is slow isn't actually the problem.
Pandas gives you a few simple tools to start with.
###  `%timeit` -- Know How Long Things Actually Take
`%timeit` is a Jupyter magic command that runs a line of code multiple times and gives you the average execution time. It's the simplest way to compare two approaches and know, concretely, which one is faster.
python

```
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'sales': np.random.randint(100, 10000, size=100_000),
    'discount': np.random.uniform(0.0, 0.5, size=100_000)
})

# Approach A
%timeit df.apply(lambda row: row['sales'] * row['discount'], axis=1)

# Approach B
%timeit df['sales'] * df['discount']
```

On a dataset of 100,000 rows, the difference is not subtle:
markup

```
1.91 s ± 228 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)316 μs ± 14 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
```

Same output. Completely different cost. That's the kind of thing you'd never notice by just running the cell once and moving on.
###  `df.info()` and `df.memory_usage()` -- Know What You're Carrying
Speed isn't just about computation. Memory plays a huge role too. A dataframe that's bloated with the wrong data types will slow everything down before you've even written a single transformation.
python

```
df.info()
```

**Output:**
markup

```
<class 'pandas.core.frame.DataFrame'>RangeIndex: 100000 entries, 0 to 99999Data columns (total 2 columns): #   Column    Non-Null Count   Dtype  ---  ------    --------------   -----   0   sales     100000 non-null  int64   1   discount  100000 non-null  float64dtypes: float64(1), int64(1)memory usage: 1.5 MB
```

To check the memory usage
python

```
df.memory_usage(deep=True)
```

**Output:**
markup

```
Index          132sales       400000discount    800000dtype: int64
```

Here, we can see that `discount` is taking up twice the space. This is because `discount` is stored as a "heavier" number type (`float64`) while `sales` is stored in a "lighter" type (`int32`).
This becomes especially important when you're working with string columns or object types that are secretly eating memory. We'll come back to this in the next section.
### The Profiling Mindset
The tools themselves are simple. The shift is in how you approach your code. Before you optimize anything, ask: _where is the time actually going?_ Measure the slow parts. Compare alternatives. Let the numbers tell you what to fix.
Because what feels slow and what is slow are often two different things entirely.
## Mistake #1: Row-wise Operations (The Silent Killer)
If there's one thing I kept seeing come up again and again while researching this topic, it was this: people looping through Pandas dataframes row by row. And I get it. It feels natural. You think about your data one row at a time, so you write code that processes it one row at a time.
The problem is, that's not how Pandas thinks.
### How Pandas Actually Works
Pandas is built on top of NumPy, which stores data in contiguous blocks of memory, column by column. This means Pandas is heavily optimized to operate on entire columns at once. When you do that, it runs fast, low-level, vectorized operations under the hood.
When you loop through rows instead, you're essentially bypassing all of that. You're dropping down into pure Python, one row at a time, with all the overhead that comes with it. On a small dataset you'll never notice. On a large one, you'll be waiting a long time.
There are two patterns that show up constantly.
### `.iterrows()`
python

```
# Calculating a discounted price row by row
discounted_prices = []

for index, row in df.iterrows():
    discounted_prices.append(row['sales'] * (1 - row['discount']))

df['discounted_price'] = discounted_prices
```

  
This works. It will give you the right answer. But on a dataframe with 100,000 rows, it is painfully slow.
python

```
%timeit [row['sales'] * (1 - row['discount']) for index, row in df.iterrows()]
```

**Output:**
markup

```
10.2 s ± 1.73 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
```

### `.apply(axis=1)`
This one is sneakier because it looks more "Pandas-like." But applying a function across axis=1 means applying it row by row, which is essentially the same problem.
python

```
%timeit df.apply(lambda row: row['sales'] * (1 - row['discount']), axis=1)
```

**Output:**
markup

```
1.5 s ± 88.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```

Faster than `.iterrows()`, but still operating row by row. Still slow.
### The Fix: Vectorized Operations
Here's the same calculation, done the way Pandas actually wants you to do it:
python

```
df['discounted_price'] = df['sales'] * (1 - df['discount'])
```

Let's time it
python

```
%timeit df['sales'] * (1 - df['discount'])
```

Output:
markup

```
688 μs ± 236 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
```

That's it. One line. No loop. No lambda. And it's roughly **14,800x faster than**`.iterrows()` and **2,180x faster than**`**.apply(axis=1)**`**.**
What's happening here is that Pandas passes the entire column to NumPy, which executes the operation at the C level across the whole array at once. No Python overhead. No row-by-row iteration. Just fast, low-level computation.
### When `.apply()` Is Actually Fine
To be fair, `.apply()` isn't always the villain. When you're applying a function column-wise (axis=0, which is the default), it's often perfectly reasonable. The issue is specifically `axis=1`, which forces row-by-row execution.
And sometimes your logic is genuinely complex enough that a clean vectorized expression isn't obvious. In those cases, `np.vectorize()` or `np.where()` can give you something closer to vectorized performance while still letting you express conditional logic clearly.
python

```
# Instead of this
df['category'] = df.apply(
    lambda row: 'high' if row['sales'] > 5000 else 'low', axis=1
)

# Do this
df['category'] = np.where(df['sales'] > 5000, 'high', 'low')
```

python

```
%timeit df.apply(lambda row: 'high' if row['sales'] > 5000 else 'low', axis=1)
%timeit np.where(df['sales'] > 5000, 'high', 'low')
```

Output:
markup

```
1.31 s ± 189 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)1.3 ms ± 180 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
```

Same result. About **1,000x faster**.
### The Rule of Thumb
If you're writing a loop over rows in Pandas, stop and ask yourself: can this be expressed as a column operation? Nine times out of ten, the answer is yes. And when it is, the performance difference is transformative.
If you're looping through rows, you're not using Pandas. You're using Python with extra steps.
## Mistake #2: Unnecessary Copies and Memory Bloat
Row-wise operations get a lot of attention when people talk about Pandas performance. Memory gets a lot less. Which is a shame, because in my experience reading about this, bloated memory is just as responsible for slow notebooks as bad computation.
Here's the thing. Pandas operations don't always modify your dataframe in place. A lot of them quietly create a brand new copy of your data behind the scenes. Do that enough times, and you're not just holding one dataframe in memory. You're holding several, all at once, without realizing it.
### The Hidden Cost of Chained Operations
Chained operations are a common culprit. They look clean and readable, but each step can generate an intermediate copy that sits in memory until garbage collection cleans it up.
python

```
# Each step here potentially creates a new copy
df2 = df[df['sales'] > 1000]
df3 = df2.dropna()
df4 = df3.reset_index(drop=True)
df5 = df4[['sales', 'discount']]
```

By the time you get to `df5`, you potentially have five versions of your data floating around in memory simultaneously. On a small dataset this is invisible. On a large one, this is how you run out of RAM.
### Temporary Columns That Stick Around
Another pattern that quietly eats memory is creating columns you only needed temporarily.
python

```
df['gross_revenue'] = df['sales'] * df['quantity']
df['tax'] = df['gross_revenue'] * 0.075
df['net_revenue'] = df['gross_revenue'] - df['tax']

# But you only actually needed net_revenue
```

`gross_revenue` and `tax` are now permanent columns in your dataframe, taking up memory for the rest of your notebook even though they were just stepping stones.
The fix is simple. Either compute directly:
python

```
df['net_revenue'] = (df['sales'] * df['quantity']) * (1 - 0.075)
```

Or drop them as soon as you're done:
python

```
df.drop(columns=['gross_revenue', 'tax'], inplace=True)
```

### Wrong Data Types Are Quietly Expensive
This one surprised me when I came across it. By default, Pandas is quite generous with how much memory it assigns to each column. Integer columns get `int64`. Float columns get `float64`. String columns become `object` type, which is one of the most memory-hungry types in Pandas.
Let's see what that actually looks like:
python

```
df = pd.DataFrame({
    'order_id': np.random.randint(1000, 9999, size=100_000),
    'sales': np.random.randint(100, 10000, size=100_000),
    'discount': np.random.uniform(0.0, 0.5, size=100_000),
    'region': np.random.choice(['north', 'south', 'east', 'west'], size=100_000)
})

df.memory_usage(deep=True)
```

Output
markup

```
Index           132order_id     400000sales        400000discount     800000region      5350066dtype: int64
```

That `region` column, which only has four possible values, is consuming 5.3MB as an `object` type. Convert it to a categorical and watch what happens:
python

```
df['region'] = df['region'].astype('category')
df.memory_usage(deep=True)
```

Output:
markup

```
Index          132order_id    400000sales       400000discount    800000region      100386dtype: int64
```

From 5.3MB down to about 100KB. For one column. The same logic applies to integer columns where you don't need the full `int64` range. If your values fit comfortably in `int32` or even `int16`, downcasting saves real memory.
python

```
df['sales'] = df['sales'].astype('int32')
df['order_id'] = df['order_id'].astype('int32')

df.memory_usage(deep=True)
```

Output:
markup

```
Index       128order_id    400000sales       400000discount    800000region      100563dtype: int64
```

A few small type changes and your dataframe is already significantly lighter. And a lighter dataframe means faster operations across the board, because there's simply less data to move around.
### The Quick Memory Check Habit
Before you run any heavy transformation, it's worth knowing what you're working with:
python

```
print(f"Memory usage: {df.memory_usage(deep=True).sum() / 1024**2:.2f} MB")
```

It takes one second and it tells you exactly how much memory your dataframe is consuming at that point. Make it a habit before and after major transformations and you'll quickly develop an intuition for when something is heavier than it should be.
### The Insight
Slow code isn't always about computation. Sometimes your notebook is slow because it's carrying far more data than it needs to, in formats that are far more expensive than necessary. Trimming memory isn't glamorous work, but it compounds. A dataframe that's lighter to store is faster to filter, faster to merge, faster to transform.
Memory and speed are not separate problems. They're the same problem.
## Mistake #3: Overusing Pandas for Everything
This one is a little different from the previous two. It's not about a specific function or a bad habit. It's about knowing the limits of your tool.
Pandas is genuinely great. For most data tasks, especially at the scale most people are working at, it's more than enough. But there's a version of Pandas usage that I kept seeing described while researching this: people reaching for Pandas by default, for everything, regardless of whether it's the right fit.
And at a certain scale, that becomes a problem.
### The Dataset
To make this real, I generated a synthetic e-commerce dataset with 1 million rows. Nothing exotic, just the kind of data you'd realistically encounter: orders, dates, regions, categories, sales figures, discounts, quantities and statuses.
python

```
import pandas as pd
import numpy as np

np.random.seed(42)

n = 1_000_000

regions = ['north', 'south', 'east', 'west']
categories = ['electronics', 'clothing', 'furniture', 'food', 'sports']
statuses = ['completed', 'returned', 'pending', 'cancelled']

df = pd.DataFrame({
    'order_id': np.arange(1000, 1000 + n),
    'order_date': pd.date_range(start='2022-01-01', periods=n, freq='1min'),
    'region': np.random.choice(regions, size=n),
    'category': np.random.choice(categories, size=n),
    'sales': np.random.randint(100, 10000, size=n),
    'quantity': np.random.randint(1, 20, size=n),
    'discount': np.round(np.random.uniform(0.0, 0.5, size=n), 2),
    'status': np.random.choice(statuses, size=n),
})

df.to_csv('large_sales_data.csv', index=False)
```

One million rows. Saved to a CSV. This is the dataset we'll be working with for the rest of the article.
### Where Pandas Starts to Struggle
Pandas loads your entire dataset into memory. That's fine when your data is a few hundred thousand rows. It starts to get uncomfortable at a few million. And beyond that, you're fighting the tool.
The other scenario is complex, nested transformations where you're stacking multiple operations, creating intermediate results, and generally asking Pandas to do a lot of heavy lifting in sequence. Each step adds overhead. The costs stack up.
Here's a realistic example using our dataset. Say you need to calculate a rolling average of sales per region, flag orders above a threshold, then aggregate by month:
python

```
# Step 1: Sort
df = df.sort_values(['region', 'order_date'])

# Step 2: Rolling average per region
df['rolling_avg'] = (
    df.groupby('region')['sales']
    .transform(lambda x: x.rolling(window=7).mean())
)

# Step 3: Flag high-value orders
df['high_value'] = df['sales'] > df['rolling_avg'] * 1.5

# Step 4: Monthly aggregation
df['month'] = pd.to_datetime(df['order_date']).dt.to_period('M')
monthly_summary = df.groupby(['region', 'month'])['sales'].sum()
```

This works. But notice that Step 2 uses `.transform(lambda x: ...)`, which carries the same row-adjacent cost we talked about earlier. On 1 million rows, this pipeline will drag. Go ahead and time it on your machine and you'll see exactly what I mean.
### What to Reach For Instead
The good news is you don't have to abandon Pandas entirely. There are a few options depending on the situation.
**Chunking**  
If your dataset is too large to load all at once, Pandas lets you process it in chunks. Instead of loading all 1 million rows into memory at once, you load and process a portion at a time:
python

```
chunk_size = 100_000
results = []

for chunk in pd.read_csv('large_sales_data.csv', chunksize=chunk_size):
    chunk['discounted_price'] = chunk['sales'] * (1 - chunk['discount'])
    results.append(chunk.groupby('region')['discounted_price'].sum())

final_result = pd.concat(results).groupby(level=0).sum()
print(final_result)
```

Instead of asking Pandas to hold 1 million rows in memory simultaneously, you're feeding it 100,000 rows at a time, processing each chunk, and assembling the results at the end. It's not the most elegant pattern, but it lets you work with data that would otherwise crash your kernel.
**When to Consider Other Tools**  
Sometimes the honest answer is that Pandas isn't the right tool for the job. This isn't a criticism, it's just scope. A few worth knowing about:
  * **Polars** : A modern dataframe library built in Rust, designed for speed. It uses lazy evaluation, meaning it optimizes your entire query before executing it. For large datasets it can be dramatically faster than Pandas.
  * **Dask** : Extends Pandas to work in parallel across multiple cores or even multiple machines. If you're comfortable with Pandas syntax, Dask feels familiar.
  * **DuckDB** : Lets you run SQL queries directly on your dataframes or CSV files with surprisingly fast performance. Great for aggregations and analytical queries on large data.


The point isn't to abandon Pandas. For most everyday data work, it's the right choice. The point is to recognize when you've hit its ceiling, and know that there are good options on the other side of it.
## The Real-World Refactor: From 61 Seconds to 0.33 Seconds
This is where everything we've covered stops being theoretical.  
I took our 1 million row e-commerce dataset and wrote the kind of Pandas code that feels completely normal. The kind of thing you'd write on a Tuesday afternoon without thinking twice.
Then I timed it.
### The Slow Version
python

```
import time

df = pd.read_csv('large_sales_data.csv')

start = time.time()

# Row-wise revenue calculation
df['gross_revenue'] = df.apply(
    lambda row: row['sales'] * row['quantity'], axis=1
)
df['tax'] = df.apply(
    lambda row: row['gross_revenue'] * 0.075, axis=1
)
df['net_revenue'] = df.apply(
    lambda row: row['gross_revenue'] - row['tax'], axis=1
)

# Row-wise flagging
df['order_flag'] = df.apply(
    lambda row: 'high' if row['net_revenue'] > 50000 else 'low', axis=1
)

# Final aggregation
result = df.groupby('region')['net_revenue'].sum()

end = time.time()
print(f"Total runtime: {end - start:.2f} seconds")
```

Output:
markup

```
Total runtime: 61.78 seconds
```

Over a minute. For a four-step pipeline. And nothing looks wrong. Let's break down exactly what's making it slow.
**Three mistakes, all in one pipeline:**
  * First, the data types are never addressed. The `region`, `category` and `status` columns load as generic `object` types, which are memory-hungry and slow to work with. We're carrying that dead weight through every single operation.
  * Second, there are three separate `.apply(axis=1)` calls just to calculate revenue. Each one loops through all 1 million rows in Python, one at a time. We already saw in Section 4 how expensive that is. Here we're doing it three times in a row.
  * Third, `gross_revenue` and `tax` are created as permanent columns even though they're just intermediate steps. They serve no purpose beyond being stepping stones to `net_revenue`, but they sit in memory for the rest of the pipeline anyway.


Here's how I'd fix this step by step
**Step 1: Fix data types upfront**  
Before anything else, convert the obvious categorical columns:
python

```
df['region'] = df['region'].astype('category')
df['category'] = df['category'].astype('category')
df['status'] = df['status'].astype('category')
```

This alone reduces memory usage significantly and makes subsequent operations cheaper across the board.
**Step 2: Replace**`.apply()`**with vectorized operations**  
Instead of three separate row-wise calls, one vectorized expression does the same work:
python

```
# Before: three .apply() calls, three passes through 1 million rows
df['gross_revenue'] = df.apply(lambda row: row['sales'] * row['quantity'], axis=1)
df['tax'] = df.apply(lambda row: row['gross_revenue'] * 0.075, axis=1)
df['net_revenue'] = df.apply(lambda row: row['gross_revenue'] - row['tax'], axis=1)

# After: one vectorized expression, no temporary columns
df['net_revenue'] = df['sales'] * df['quantity'] * (1 - 0.075)
```

**Step 3: Replace row-wise flagging with**`np.where()`
python

```
# Before
df['order_flag'] = df.apply(
    lambda row: 'high' if row['net_revenue'] > 50000 else 'low', axis=1
)

# After
df['order_flag'] = np.where(df['net_revenue'] > 50000, 'high', 'low')
```

Same logic. Vectorized. Done.
### The Fast Version
Put it all together and the pipeline looks like this:
python

```
import time

df = pd.read_csv('large_sales_data.csv')

start = time.time()

# Fix 1: Correct data types upfront
df['region'] = df['region'].astype('category')
df['category'] = df['category'].astype('category')
df['status'] = df['status'].astype('category')

# Fix 2: Vectorized revenue calculation, no temporary columns
df['net_revenue'] = df['sales'] * df['quantity'] * (1 - 0.075)

# Fix 3: Vectorized flagging with np.where
df['order_flag'] = np.where(df['net_revenue'] > 50000, 'high', 'low')

# Final aggregation
result = df.groupby('region')['net_revenue'].sum()

end = time.time()
print(f"Total runtime: {end - start:.2f} seconds")
```

Output:
markup

```
Total runtime: 0.33 seconds
```

61.78 seconds down to 0.33 seconds. A **99.5% reduction in runtime**. That's like **187x faster**.
It's not a trick. That's just how Pandas is supposed to be used.
## Before You Run Your Next Notebook
Everything we covered comes down to a few core habits. Not rules. Not tricks. Just a different way of thinking about your code before you write it.
  * **Think in columns, not rows.** If you're looping through a dataframe row by row, stop and ask whether the same thing can be expressed as a column operation. Nine times out of ten, it can.
  * **Measure before you optimize.** Don't guess where the slowness is coming from. Use `%timeit` and `df.memory_usage()` to let the numbers tell you what to fix.
  * **Watch your memory, not just your speed.** Wrong data types, unnecessary copies and temporary columns all add up. A lighter dataframe is a faster dataframe.
  * **Know when to switch tools.** Pandas is the right choice most of the time. But at a certain scale, the right optimization is recognizing that you've outgrown it.


I started this rabbit hole because I kept seeing the same conversation come up in data communities. People frustrated with slow notebooks, code that worked fine on small data and fell apart on real data. I wanted to understand why.
What I found was that the code wasn't broken. It just wasn't built to scale. And the gap between code that works and code that works efficiently isn't about being an advanced Pandas user. It's about a handful of habits applied consistently.
If you've ever waited too long for a notebook to finish and just assumed that was normal, now you know it doesn't have to be.
If this changed how you think about your Pandas code, I'd love to hear what bottlenecks you've been dealing with. Feel free to say hi on any of these platforms
[Medium](https://medium.com/@ibbysalam)
[LinkedIn](https://www.linkedin.com/in/ibrahim-salami-059863228/)
[Twitter](https://x.com/IbbySalam)
[YouTube](https://www.youtube.com/@ibbysalam)
## Register Now to Continue Reading
### Unlock Towards Data Science with a Free Account
All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more.
Register for freeSign in to your account
* * *
![](../images/body-542334df68a32cd56c4a.jpeg)
Written By
## [Ibrahim Salami](https://towardsdatascience.com/author/ibrahim.salami/)
[See all from Ibrahim Salami](https://towardsdatascience.com/author/ibrahim.salami/)
[Data Science](https://towardsdatascience.com/tag/data-science/)[Pandas](https://towardsdatascience.com/tag/pandas/)[Productivity](https://towardsdatascience.com/tag/productivity/)[Python](https://towardsdatascience.com/tag/python/)[Vectorization](https://towardsdatascience.com/tag/vectorization/)
Share This Article
  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fi-reduced-my-pandas-runtime-by-95-heres-what-i-was-doing-wrong%2F&title=I%20Reduced%20My%20Pandas%20Runtime%20by%2095%25%20%E2%80%94%20Here%E2%80%99s%20What%20I%20Was%20Doing%20Wrong)
  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fi-reduced-my-pandas-runtime-by-95-heres-what-i-was-doing-wrong%2F&title=I%20Reduced%20My%20Pandas%20Runtime%20by%2095%25%20%E2%80%94%20Here%E2%80%99s%20What%20I%20Was%20Doing%20Wrong)
  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fi-reduced-my-pandas-runtime-by-95-heres-what-i-was-doing-wrong%2F&text=I%20Reduced%20My%20Pandas%20Runtime%20by%2095%25%20%E2%80%94%20Here%E2%80%99s%20What%20I%20Was%20Doing%20Wrong)


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.
[Write for TDS](https://towardsdatascience.com/submissions/)
## Related Articles
  * [![](../images/001.jpg)](https://towardsdatascience.com/5-different-ways-to-save-your-machine-learning-model-b7996489d433/)
## [5 Different Ways To Save Your Machine Learning Model](https://towardsdatascience.com/5-different-ways-to-save-your-machine-learning-model-b7996489d433/)
[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)
Simplifying The Process of Reusing Models
[Kurtis Pykes](https://towardsdatascience.com/author/kurtispykes/)
May 18, 20224 min read
  * [![](../images/002.jpg)](https://towardsdatascience.com/sql-unit-testing-in-bigquery-here-is-a-tutorial-64fc7a6b377/)
## [SQL Unit Testing in BigQuery? Here is a tutorial.](https://towardsdatascience.com/sql-unit-testing-in-bigquery-here-is-a-tutorial-64fc7a6b377/)
[Data Science](https://towardsdatascience.com/category/data-science/)
Complete guide for scripting and UDF testing.
[Mike Shakhomirov](https://towardsdatascience.com/author/mshakhomirov/)
December 1, 20204 min read
  * [![](../images/003.jpg)](https://towardsdatascience.com/the-ultimate-data-science-prerequisite-learning-list-348ce89805f9/)
## [The Ultimate Data Science Prerequisite Learning List](https://towardsdatascience.com/the-ultimate-data-science-prerequisite-learning-list-348ce89805f9/)
[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)
See the best places to learn Linear Algebra, Calculus, Statistics, Probability, and Programming.
[Dario Radečić](https://towardsdatascience.com/author/radecicdario/)
October 3, 20195 min read
  * [![](../images/004.jpeg)](https://towardsdatascience.com/how-and-why-performing-one-hot-encoding-in-your-data-science-project-a1500ec72d85/)
## [How and Why Performing One-Hot Encoding in Your Data Science Project](https://towardsdatascience.com/how-and-why-performing-one-hot-encoding-in-your-data-science-project-a1500ec72d85/)
[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)
An article on what one-hot encoding is, why to use it, and how to do it (in Python)
[Federico Trotta](https://towardsdatascience.com/author/federicotrotta/)
June 27, 20225 min read
  * [![](../images/005.png)](https://towardsdatascience.com/why-and-how-to-adjust-p-values-in-multiple-hypothesis-testing-2ccf174cdbf8/)
## [Why and How to Adjust P-values in Multiple Hypothesis Testing](https://towardsdatascience.com/why-and-how-to-adjust-p-values-in-multiple-hypothesis-testing-2ccf174cdbf8/)
[Data Science](https://towardsdatascience.com/category/data-science/)
P-values below a certain threshold are often used as a method to select relevant features. Advice below suggests how to use them correctly.
[Igor Šegota](https://towardsdatascience.com/author/igor-s/)
May 5, 20239 min read
  * [![](../images/006.jpg)](https://towardsdatascience.com/args-and-kwargs-in-python-function-calling-made-easy-acfe736f988a/)
## [Args and Kwargs in Python - Function Calling Made Easy](https://towardsdatascience.com/args-and-kwargs-in-python-function-calling-made-easy-acfe736f988a/)
[Programming](https://towardsdatascience.com/category/programming/)
Understand how functions can return multiple values in full.
[Yash Prakash](https://towardsdatascience.com/author/ipom/)
January 12, 20215 min read
  * [![](../images/007.jpeg)](https://towardsdatascience.com/the-power-and-danger-of-intuition-in-data-science-bb5c04471f58/)
## [The Power and Danger of Intuition in Data Science](https://towardsdatascience.com/the-power-and-danger-of-intuition-in-data-science-bb5c04471f58/)
[Data Science](https://towardsdatascience.com/category/data-science/)
As I start my new journey as a career Data Scientist, I had to cross paths with my old hypothetical nemesis: The Monty Hall Problem. To...
[Andrew Mooney](https://towardsdatascience.com/author/andrew-k-mooney/)
December 17, 20208 min read
  * [![](../images/008.jpg)](https://towardsdatascience.com/bringing-the-best-out-of-jupyter-notebooks-for-data-science-f0871519ca29/)
## [Bringing the best out of Jupyter Notebooks for Data Science](https://towardsdatascience.com/bringing-the-best-out-of-jupyter-notebooks-for-data-science-f0871519ca29/)
[Programming](https://towardsdatascience.com/category/programming/)
Enhance Jupyter Notebook's productivity with these Tips & Tricks.
[Parul Pandey](https://towardsdatascience.com/author/pandeyparul/)
December 19, 20188 min read
  * [![](../images/009.jpg)](https://towardsdatascience.com/tsv-in-pandas-a-how-to-guide-1854b4b86c7f/)
## [TSV in Pandas: A How-To Guide](https://towardsdatascience.com/tsv-in-pandas-a-how-to-guide-1854b4b86c7f/)
[Programming](https://towardsdatascience.com/category/programming/)
The Correct Way of Loading and Writing TSV Files with Pandas
[Michał Marcińczuk](https://towardsdatascience.com/author/czuk/)
October 1, 20245 min read


  * [YouTube](https://www.youtube.com/c/TowardsDataScience)
  * [X](https://x.com/TDataScience)
  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)
  * [Threads](https://www.threads.net/@towardsdatascience)
  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)


[](https://towardsdatascience.com/)
Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.
Subscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)
© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)
