0
0

PythonでPandas DataFrameをAzure Table Storageに書き込む

Posted at

PandasのDataFrameをそのままAzure Table Storageに書き込むサンプルコードです。
対象のDataFrameにTable Storageで必須となるカラム「PartitionKey」「RowKey」を含めます。これらはTable Storageの制限として2つの組み合わせで一意になる必要があります。詳しくは公式ドキュメント参照。サンプルではめんどくさいのでramdomにしていますが、検索パフォーマンスに大きく影響するので実際に利用する場合はちゃんと設計しましょう。
※テーブルはすでに作成されている前提です。

import random
import pandas as pd

from azure.cosmosdb.table.models import Entity
from azure.cosmosdb.table.tableservice import TableService

CONNECTION_STRING = ""
table_name = ""

# 適当にDataFrameを作成
cols = ["PartitionKey", "RowKey", "val3", "val4"]
df = pd.DataFrame(index=[], columns=cols)
for i in range(50, 100):
    row = pd.Series([str(i).zfill(4), str(0), random.random() * 100, random.random() * 1000], index=["PartitionKey", "RowKey", "val3", "val4"])
    df = df.append(row, ignore_index=True)

# 書き込み
table_service = TableService(connection_string=CONNECTION_STRING)
for index, row in df.iterrows():
    table_service.insert_entity(table_name, row.to_dict())
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0