日次の獲得コホートを割り当てる
動画で見たとおり、コホートを定義することがコホート分析の第一歩です。ここでは、各顧客が最初に取引した日にもとづいて、日次コホートを作成します。
データは online という DataFrame として読み込まれています。コンソールで online.head() を実行して先頭行を確認できます。
この演習はコースの一部です
Pythonで学ぶカスタマーセグメンテーション
演習の手順
xから年・月・日を抽出する datetime オブジェクト用の関数を作成します。InvoiceDate列にget_day関数を適用して、InvoiceDay列を作成します。CustomerIDでグループ化し、後続の計算に使うためにInvoiceDayを選択するgroupbyオブジェクトを作成します。- 最小の
InvoiceDayを取り出してCohortDay列を作成します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define a function that will parse the date
def get_day(x): return dt.datetime(x.year, ____, ____)
# Create InvoiceDay column
online['____'] = online['____'].apply(____)
# Group by CustomerID and select the InvoiceDay value
grouping = online.groupby('____')['____']
# Assign a minimum InvoiceDay value to the dataset
online['CohortDay'] = grouping.____('____')
# View the top 5 rows
print(online.head())