Parallel Process Data Frames Using Dask

When dealing with data frames, sometimes it is prolonged to process them when there are a massive amount of data. In that case, it is better to use the parallel process to speed up the entire process. Dask is an open-source library that allows us to accomplish the above goal. 


### Dask - Why do we need it?
For data science tasks, numpy, sklean, pandas, etc., are very helpful to manipulate data. In most the cases, pandas library is sufficient. We can do various kinds of things using pandas.
The problem starts when our data gets more extensive. Because our RAM may not be enough moreover, it can take much time.
I am sure you guys know about big data platforms likes Hadoop and spark. However, unfortunately, those are not python environments, and many people prefer Pandas, TensorFlow, Torch, etc., for work with ML/DL tasks. 

### Parallel Processing
Executing multiple tasks at the same time is called parallel processing. In typical situations, code is executed sequentially, which means 1 task at a time. 
Rather than waiting for the prior task to finish, we can compute many levels concurrently at the same interval.

### Demo
Let us compare the time taken for some tasks using Pandas and Dask. 
First, we need to install dask library.  Then we need to import pandas and dask. 

![Screen Shot 2021-09-02 at 17.51.10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/29527612-a964-69aa-11f7-1bc350ec35cf.png)

I used two large CSV files. The first file is around 8GB, and the second one is around 5 GB.

#### 1) Read file and Create a data frame.

![Screen Shot 2021-09-02 at 17.54.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/e6c6bbf5-8152-c444-141e-3c51f428bf3a.png)
Pandas took approximately 2 minutes and 30 seconds to read and creating the data frame. I feel thrilled to have a powerful server. 
Now let us see how the dask performs.
![Screen Shot 2021-09-02 at 17.54.43.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/0d9fd420-b2df-e63b-e0ea-24b31b7346f1.png)
**OH, WAIT. 0.7 milliseconds ???? **

Should be something wrong ....
No, there is nothing wrong.
Dask is a beast. It took only 0.7 milliseconds to read the same file that pandas took more than 2 minutes. 

#### 2) Append two Data frames.
![Screen Shot 2021-09-02 at 17.55.06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/fc09086f-8a49-de7f-12c1-2b31aa67cc61.png)
OK, for appending, we need two data frames. So let us read another file from pandas, create a second data frame, and append it to the first data frame.

For the first file - 2 minutes and 30 seconds
For the second file - 1 minute and 34 seconds
Append process - 19 seconds

So it took a total of approximately 4 minutes and 20 seconds.
OK, now it is time to see how the dask performs.
![Screen Shot 2021-09-02 at 17.55.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/c79fcaa8-81dc-89bf-395a-3209376cef29.png)
For the first file - 07 milliseconds
For the second file - 02 milliseconds
Append process - 03 milliseconds

It took 12 milliseconds for the whole process.

**MILLISECONDS ???**

Yes, you have seen it correctly. We have to compare minutes and milliseconds. It is awesome.


#### 3) Merge two Data frames.
![Screen Shot 2021-09-02 at 17.56.15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/e32e0f1b-75ed-a5b6-b7ca-5c5fa11f1360.png)
Pandas could not handle it. My kernel restarted after around 20 minutes of running due to the out-of-memory. I have 120 GB of memory. However, it was not enough for pandas. 
![Screen Shot 2021-09-02 at 17.56.26.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/481cc60f-4a71-e9ce-9fe5-32cc34e208bd.png)
WoW.
It is finished in just less than a second. Actually, it took only 59 milliseconds. I have waited 20 minutes for pandas, and even it could not finish the task.

#### 4) Group Data
![Screen Shot 2021-09-02 at 17.55.37.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/ec89089f-62e7-ac9c-2ad7-f8793fda982e.png)
![Screen Shot 2021-09-02 at 17.55.46.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/20a3f772-a0b6-0b0f-17dd-78b53e3c49f7.png)

For my data set, there is not much of a difference. Both finished very quickly.

#### 5) Get unique values
![Screen Shot 2021-09-02 at 17.57.17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/9630eea5-1fc3-ecbb-b5f6-20f5b5c8d65f.png)
![Screen Shot 2021-09-02 at 17.57.30.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/5d4e7127-d416-b44e-dca9-a5e07ec31a47.png)
Pandas took 4 seconds. However, dask does not support the same command. There can be a workaround or other command. However, I will skip it.

#### 6) Get notNA values
![Screen Shot 2021-09-02 at 17.56.44.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/6402f2d3-b493-8f9a-eeee-f76a2f9c2b72.png)
![Screen Shot 2021-09-02 at 17.57.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/acdad5cb-ac34-6a85-4fbf-bc28de75ebd4.png)
Pandas took 32 seconds. And for the dask it's the same as the above. Dask does not support the same command. There can be a workaround or other command. However, I will skip it.

#### 7) Sorting
![Screen Shot 2021-09-02 at 19.06.09.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/730de123-d9a7-4379-f7d4-f36233a18964.png)

![Screen Shot 2021-09-02 at 17.56.35.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/3193ec47-87bc-a834-8304-57543f505fbc.png)
For the sorting, pandas is the winner. It took two and half minutes while dask took three and half minutes.  


#### End Note
Please test with some other tasks with dask and see how it performs. We can see that there are some advantages and disadvantages of dask. 
The conclusion is, it is better to combine both when working with a dataset. Because we can see dask is so much faster but has some limitations for the people who worked with pandas. 


*本記事は @qualitia_cdevの中の一人、@nuwanさんが書いてくれました。
*This article is written by @nuwan a member of @qualitia_cdev.