始める無料で始める

日次の獲得コホートを割り当てる

動画で見たとおり、コホートを定義することがコホート分析の第一歩です。ここでは、各顧客が最初に取引した日にもとづいて、日次コホートを作成します。

データは 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())
コードを編集して実行