BigQueryでODを示すプローブデータを生成する

BigQueryでODを示すプローブデータを生成します.

# 架空の行動履歴データの作成

import random
import datetime
import pandas as pd

# 重複なしランダム発生
def rand_ints_nodup(a, b, k):
    ns = []
    while len(ns) < k:
        n = random.randint(a, b)
        if not n in ns:
            ns.append(n)
    return ns

# 架空の行動履歴作成
def make_probe(_list, s, e):
    for _id in range(s, e):
        # 立ち寄りスポット数
        spot_num = random.randint(1, 5)
        # 立ち寄りスポットリスト
        spot_list = rand_ints_nodup(1, 5, spot_num)

        # 日付
        dt = datetime.datetime(2018, 2, 1, 9, 15, 30)
        # 日にち加算
        dp = random.randint(1, 20)
        dt = dt + datetime.timedelta(days=dp)
        date = str(dt).split('-')[1] + str(dt).split('-')[2].split(' ')[0]
        # 時刻加算
        hp = random.randint(1, 10)
        dt = dt + datetime.timedelta(hours=hp)

        for spot in spot_list:
            dt = dt + datetime.timedelta(minutes=1)
            _list.append([date, _id+1, dt, f'spot_{spot}'])
    return _list

# 行動履歴作成
probe_list = []
probe_list = make_probe(probe_list, 0, 400)
probe_list = make_probe(probe_list, 30, 140)

# データフレーム化
df = pd.DataFrame(probe_list, columns=['date', 'id', 'o_time', 'o_spot'])
df.head()

# index date	id	o_time	o_spot
# 0	0211	1	2018-02-11 18:16:30	spot_1
# 1	0211	1	2018-02-11 18:17:30	spot_2

# OD行動履歴データの生成

LAGを使ってOD行動履歴を作成します。

  • LAG(カラム, 1) : カラムで1行ずらす
  • OVER (PARTITION BY date, id): 「date, id」ごとにずらす
  • OVER (ORDER BY id, o_time) : 「id, o_time」ごとにソートした上でずらす
query = '''
SELECT 
    * ,
    LAG(o_time, 1) OVER (PARTITION BY date, id ORDER BY id, o_time) AS d_time,
    LAG(o_spot, 1) OVER (PARTITION BY date, id ORDER BY id, o_time) AS d_spot
FROM 
    probe
ORDER BY date, id o_time
'''

# まとめ

BigQueryでODを示すプローブデータを生成しました

# 参考サイト

【BigQuery】LAG関数,LEAD関数の使い方 (opens new window)

BigQuery ScriptingでPythonっぽいループ処理をしてみた (opens new window)

国勢調査から2重棒グラフを作成する

国勢調査から2重棒グラフを作成する

国勢調査から2重棒グラフを作成します.

pandasでテキストファイルを読み込む

pandasでテキストファイルを読み込む

pandasでテキストファイルを読み込みを実装します.