ETL と ELT パイプラインの構築

Python で学ぶ ETL と ELT

Jake Roach

Data Engineer

CSV からデータを抽出

import pandas as pd

# Read in the CSV file to a DataFrame
data_frame = pd.read_csv("raw_data.csv")
# Output the first few rows
data_frame.head()
   name         num_firms   total_income
0  Advertising     58         3892.41
1  Apparel         39         5422.69
...
49 Trucking        35         17324.36

read_csv()

  • ファイルパスを受け取り、DataFrame を返す
  • delimiterheaderengine

$$

.head()

  • DataFrame の先頭 n 行を表示
Python で学ぶ ETL と ELT

DataFrame のフィルタリング

   name         num_firms   total_income
0  Advertising     58         3892.41
1  Apparel         39         5422.69
...
49 Trucking        35         17324.36

         name         num_firms
      1  Apparel         39
      37 Apparel         61

# First, by rows
data_frame.loc[data_frame["name"] == "Apparel", :]

# Then, by columns
data_frame.loc[:, ["name", "num_firms"]]

.loc

  • DataFrame をフィルタ
  • : は「全て」
Python で学ぶ ETL と ELT

DataFrame を CSV に書き出す

# Write a DataFrame to a .csv file
data_frame.to_csv("cleaned_data.csv")

.to_csv()

  • path を受け取り、その場所にファイルを書き出す
  • 出力を調整するパラメータも指定可能

$$

その他の出力:

.to_json().to_excel().to_sql()

Python で学ぶ ETL と ELT

SQL クエリを実行する

data_warehouse.execute(  # Use Python clients or other tools to run SQL queries
    """
    CREATE TABLE total_sales AS 
        SELECT
            ds,
            SUM(sales)
        FROM raw_sales_data
        GROUP BY ds;
    """
)
  • .execute() などで SQL を実行
Python で学ぶ ETL と ELT

まとめて実行

# Define extract(), transform(), and load() functions
...

def transform(data_frame, value):
    return data_frame.loc[data_frame["name"] == value, ["name", "num_firms"]]

# First, extract data from a .csv
extracted_data = extract(file_name="raw_data.csv")

# Then, transform the `extracted_data`
transformed_data = transform(data_frame=extracted_data, value="Apparel")

# Finally, load the `transformed_data`
load(data_frame=transformed_data, file_name="cleaned_data.csv")
Python で学ぶ ETL と ELT

Let's practice!

Python で学ぶ ETL と ELT

Preparing Video For Download...