Pandas: How to Process a Dataframe in Parallel | by Konstantinos Patronas | Feb, 2022

Make pandas lightning quick

Pandas is essentially the most extensively used library for knowledge evaluation however its moderately sluggish as a result of pandas doesn’t make the most of multiple CPU core, to hurry up issues and make the most of all cores we have to break our knowledge body to smaller ones, ideally in elements of equal to the variety of out there CPU cores.

Python concurrent.futures permits as to simply create processes with out the necessity to fear for stuff like becoming a member of processes and many others, think about the next instance (

And the CSV file that we’ll use it to create the Dataframe

Explaining the Code

These are the libraries we’d like, concurrent.futures is the one that gives what we have to execute course of the information body in parallel

The do_something perform accepts a Dataframe as parameter, this perform might be executed as a separate processes in parallel

The bellow capabilities return the Dad or mum PID and the present course of PID


The pandas operation we carry out is to create a brand new column named diff which has the time distinction between present date and the one within the “Order Date” column. After the operation, the perform returns the processed Knowledge body

The bellow a part of the code is definitely the beginning and initiation a part of our script

  • line 27 defines the variety of parallel processes primarily based on the variety of CPU cores (logical or not) except a parameter of what number of processes to be created have been offered.
  • line 32 splits the Dataframe in smaller knowledge frames, equal to the variety of num_procs which has been assigned in line 27

This half makes use of the ProcessPoolExecutor to create an executor object that can create quite a few parallel processes equal to num_procs.

  • line 35 begins the processes with every course of executing the do_something perform with a Dataframe from the splitted_df knowledge frames as parameter
  • line 36 waits for all processes to be accomplished and appends the returned Dataframe from the do_something perform to the df_results checklist

Final however not least, line 45 concatenates all pandas Dataframes to a single Dataframe.


  • Utilizing a number of processes has its limitations, the best variety of processes ought to be equal to the variety of CPU cores.
  • Creating processes means that there’s an intensive reminiscence overhead.
  • Processing Dataframes in parallel will be tough! multiprocessing could cause improper ends in case {that a} calculation of rows requires knowledge from different Dataframes that processed in parallel.


Lets see the efficiency of parallel processing in my 8 core machine

  • There’s a efficiency improve so long as we do not exceed the variety of cores
  • Efficiency improve shouldn’t be linear and tends to be smaller as we create extra processes
  • Once we exceed the variety of cores the efficiency will get worse

I hope you discovered this text helpful and assist you create tremendous quick Pandas functions 🙂

More Posts