How can I make my Python code faster when working with a lot of data?

I’m working on a Python program that handles thousands of records. It filters the data, does some calculations, and groups the results. Everything works fine with smaller datasets, but it starts getting pretty slow when the amount of data increases. What are some simple ways to find out what’s slowing it down and improve the performance without overcomplicating the code?

1 Like

Are you filtering and grouping records at the database level?

Hi @krishangtechnolb

Are you using pandas?

Are numbers stored as string, integers, or floats? The data type can affect performance.

Happy coding

What’s the time complexity of your calculations? Avoid nested loops if possible. Trying to lower the O(whatever you’re doing)

I’d suggest running cProfile to see which part of your code is taking the longest. Often the slowdown comes from nested loops or inefficient data types. If you’re using pandas or NumPy, try vectorized operations instead of manual iteration. Also, make sure numbers aren’t stored as strings, since conversions add overhead. Once you know the bottleneck, you can target that specifically.

I’d start with Python’s built-in cProfile to see which functions are taking the most time. Also check for unnecessary loops and repeated calculations. For larger datasets, using sets or dictionaries for lookups can make a big difference. Optimize the slowest part first rather than rewriting everything.

1 Like

First, use tools like cProfile or timeit to find the slowest part. Then reduce unnecessary loops, filter data early, avoid repeated calculations, and use efficient structures like dictionaries or sets. Always measure performance before and after changes.

1 Like

Yeah, I’m using pandas for the processing. Most of the numbers are integers or floats, but I’ll check the data types again to make sure some of them aren’t being treated as strings. I hadn’t thought that could slow things down.

Since you are using pandas, data type optimization will give you a massive, instant speedup!

When pandas reads data (like from a CSV), it often defaults to the heavy 64-bit types (int64 or float64) or treats mixed columns as slow object (string) types.

Here are two quick things you can do right now to make your code faster:

  1. Check and Downcast Types: Run df.info() to see your column types. If you have integers that don’t exceed 2 billion, downcast them from int64 to int32 (or even int16). For strings, explicitly convert them using .astype('category'). This drastically reduces memory usage and speeds up processing.
  2. Ditch the .iloc, .loc, or .iterrows() loops: If you are filtering or doing calculations inside a manual loop, it completely defeats the purpose of pandas. Always use vectorized operations (e.g., df['result'] = df['A'] * df['B']) or pandas’ built-in .groupby() and .agg() methods. They run on highly optimized C code under the hood.

Give data type casting a shot and let us know if the processing time drops!


I’m looping through the records and then checking related values inside another loop, so it can get close to O(n²) as the dataset grows. I’m looking at whether I can replace that with pandas groupby() and vectorized operations instead.

I haven’t run cProfile yet, so I’ll give that a try first and see where the actual bottleneck is. I’m also going to check the data types and see if I can replace some of the loops with pandas vectorized operations. That should give me a better idea of what’s really slowing things down

I’ll run cProfile first and look at the cumtime column to see which functions are actually taking up most of the time. I’ll also check for repeated calculations and see if sets or dictionaries could make the lookups faster. I’d rather find the real bottleneck first instead of changing the whole code blindly.

I checked the data and found that the price and quantity columns are being treated as text even though they contain numbers. I’m going to fix those types and replace some of the loops with pandas’ built-in functions. Then I’ll compare the processing time before and after to see how much it helps.

If you are doing nested loops to compare or find related records across your dataset, merge or merge_asof usually replaces that pattern cleanly. Under the hood, pandas uses hash joins or binary search, which drops the time complexity from quadratic to roughly linear or log-linear.

For the grouping and calculations, groupby().agg() or transform() handles aggregations in C/Cython without Python-level iteration. If you still have custom logic that cannot be vectorized directly, apply() on a grouped object is often faster than manual loops, though native vectorization or numpy functions are best where possible.

For data types, converting low-cardinality string columns to category can cut memory usage and speed up grouping operations substantially.

If you want to stick with standard Python without extra dependencies, collections.defaultdict speeds up grouping compared to checking keys in a regular dictionary. For filtering and transformations, list comprehensions run faster than appending inside a loop. If you can use third-party packages, polars or pandas will process tabular records much faster than pure Python.

Consider libraries like Pandas or Polars for this kind of data manipulation. They are built specifically for handling large datasets efficiently and run operations much faster than standard Python loops. If memory becomes a bottleneck, use generators or generator expressions instead of standard lists, as they process items lazily without loading the entire dataset into memory at once.

1 Like