Dask DataFrames

Python में Dask के साथ Parallel Programming

James Fulton

Climate Informatics Researcher

pandas DataFrames बनाम Dask DataFrames

import pandas as pd

# Read a single csv file pandas_df = pd.read_csv( "dataset/chunk1.csv" )

यह तुरंत एक CSV फ़ाइल पढ़ता है.

import dask.dataframe as dd

# Lazily read all csv files dask_df = dd.read_csv( "dataset/*.csv" )

यह dataset फ़ोल्डर की सभी CSV फ़ाइलें लेज़ी तरीके से पढ़ता है.

Python में Dask के साथ Parallel Programming

Dask DataFrames

print(dask_df)
Dask DataFrame Structure:
              ID     col1     col2    col3     col4     ...
npartitions=3                               
           int64   object   object   int64  float64     ...
             ...      ...      ...     ...      ...     ...
             ...      ...      ...     ...      ...     ...
             ...      ...      ...     ...      ...     ...
Dask Name: getitem, 3 tasks
Python में Dask के साथ Parallel Programming

Dask DataFrame टास्क ग्राफ

dask.visualize(dask_df)

टास्क ग्राफ 3 लोडिंग ऑपरेशन्स दिखाता है.

Python में Dask के साथ Parallel Programming

ब्लॉक्स का आकार नियंत्रित करना

# Set the maximum memory of a chunk
dask_df = dd.read_csv("dataset/*.csv", blocksize="10MB")

print(dask_df)
Dask DataFrame Structure:
              ID     col1     col2    col3     col4     ...
npartitions=7                               
           int64   object   object   int64  float64     ...
             ...      ...      ...     ...      ...     ...
             ...      ...      ...     ...      ...     ...
             ...      ...      ...     ...      ...     ...
Dask Name: getitem, 7 tasks
Python में Dask के साथ Parallel Programming

Partitions की व्याख्या

# Set the maximum memory of a chunk
dask_df = dd.read_csv("dataset/*.csv", blocksize="10MB")

7 partitions क्यों?

size      file
  9M      dataset/chunk1.csv
 18M      dataset/chunk2.csv
 32M      dataset/chunk3.csv
Python में Dask के साथ Parallel Programming

Partitions की व्याख्या

# Set the maximum memory of a chunk
dask_df = dd.read_csv("dataset/*.csv", blocksize="10MB")

7 partitions क्यों?

size      file
  9M      dataset/chunk1.csv    # 1 partition बनता है
 18M      dataset/chunk2.csv    # 2 partitions बनते हैं
 32M      dataset/chunk3.csv    # 4 partitions बनते हैं
Python में Dask के साथ Parallel Programming

Dask DataFrames से विश्लेषण

  • कॉलम चुनें
    col1 = dask_df['col1']
    
  • कॉलम असाइन करना
    dask_df['double_col1'] = 2 * col1
    
  • गणितीय ऑपरेशन्स, जैसे
    dask_df.std()
    dask_df.min()
    
  • Groupby
    dask_df.groupby(col1).mean()
    
  • वे फंक्शंस भी जिन्हें आपने पहले उपयोग किया
    dask_df.nlargest(n=3, columns='col1')
    
Python में Dask के साथ Parallel Programming

Datetimes और अन्य pandas सुविधाएँ

import pandas as pd

# Converting string to datetime format
pd.to_datetime(pandas_df['start_date'])


# Accessing datetime attributes pandas_df['start_date'].dt.year pandas_df['start_date'].dt.day pandas_df['start_date'].dt.hour pandas_df['start_date'].dt.minute
import dask.dataframe as dd

# Converting string to datetime format
dd.to_datetime(dask_df['start_date'])


# Accessing datetime attributes dask_df['start_date'].dt.year dask_df['start_date'].dt.day dask_df['start_date'].dt.hour dask_df['start_date'].dt.minute
Python में Dask के साथ Parallel Programming

परिणामों को non-lazy बनाना

# Show 5 rows
print(dask_df.head())
        ID     double_col1     col1    col2     col3     ...
0   543795              20       10     436        0     ...
1   874535              24       12     268        0     ...
2   781326              62       31     211        0     ...
3   112457              18        9     898        1     ...
4   103256             142       71     663        0     ...
# Convert lazy Dask DataFrame to in-memory pandas DataFrame
results_df = df.compute()
Python में Dask के साथ Parallel Programming

उत्तर सीधे फ़ाइल में लिखना

# 7 partitions (chunks) so 7 output files
dask_df.to_csv('answer/part-*.csv')
part-0.csv
part-1.csv
part-2.csv
part-3.csv
part-4.csv
part-5.csv
part-6.csv
Python में Dask के साथ Parallel Programming

तेज़ फ़ाइल फ़ॉर्मेट - Parquet

# Read from parquet
dask_df = dd.read_parquet('dataset_parquet')

# Save to parquet
dask_df.to_parquet('answer_parquet')
  • Parquet फ़ॉर्मेट से डेटा पढ़ना CSV से कई गुना तेज़ है
  • लिखना भी तेज़ हो सकता है
Python में Dask के साथ Parallel Programming

अभ्यास करते हैं!

Python में Dask के साथ Parallel Programming

Preparing Video For Download...