以天數計算時間差——第 1 部分
為每筆交易計算時間差,可以讓你用可比較的方式彙整各個同批群(cohort)的指標。
首先,我們要建立 6 個變數,分別擷取發票日期與同批群日期(Invoice 與 Cohort Date)的年份、月份、日期整數值。使用已為你定義好的 get_date_int() 函式:
def get_date_int(df, column):
year = df[column].dt.year
month = df[column].dt.month
day = df[column].dt.day
return year, month, day
online 資料已載入。你可以呼叫 online.head(),在主控台列印其前幾列。
本練習屬於課程
Python 的客群分群
練習說明
- 在
InvoiceDay欄位上呼叫get_date_int函式,建立invoice_year、invoice_month、invoice_day物件。 - 在
CohortDay欄位上呼叫get_date_int函式,建立cohort_year、cohort_month、cohort_day物件。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get the integers for date parts from the `InvoiceDay` column
____, ____, ____ = get_date_int(____, ____)
# Get the integers for date parts from the `CohortDay` column
____, ____, ____ = get_date_int(____, ____)