Python で学ぶ ETL と ELT
Jake Roach
Data Engineer
下流の利用者に価値を届けるには、データを適切に変換する必要があります

pandas は表形式データの変換に強力です
.loc[].to_datetime().loc[] は DataFrame の両次元を同時に変換できます
# 0 以外の行のみ保持
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, :]
# 余分な列を削除
cleaned = raw_stock_data.loc[:, ["timestamps", "open", "close"]]
# 1 行でまとめる
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, ["timestamps", "open", "close"]]
.iloc[] は整数インデックスで DataFrame を抽出します
cleaned = raw_stock_data.iloc[[0:50], [0, 1, 2]]
下流の用途に合わせて、データ型の変換が必要になることが多いです
.to_datetime()# 「timestamps」列の現在の例: "20230101085731"
# 「timestamps」列を datetime 型に変換
cleaned["timestamps"] = pd.to_datetime(cleaned["timestamps"], format="%Y%m%d%H%M%S")
Timestamp('2023-01-01 08:57:31')
# 「timestamps」列の現在の例: 1681596000011
# 「timestamps」列を datetime 型に変換
cleaned["timestamps"] = pd.to_datetime(cleaned["timestamps"], unit="ms")
Timestamp('2023-04-15 22:00:00.011000')
変換にはリスクがあります:
# DataFrame を確認する方法
cleaned = raw_stock_data.loc[raw_stock_data["open"] > 0, ["timestamps", "open", "close"]]
print(cleaned.head())
# 最小・最大のレコードを確認
print(cleaned.nsmallest(10, ["timestamps"]))
print(cleaned.nlargest(10, ["timestamps"]))
Python で学ぶ ETL と ELT