開始使用免費開始

以天數計算時間差——第 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_yearinvoice_monthinvoice_day 物件。
  • CohortDay 欄位上呼叫 get_date_int 函式,建立 cohort_yearcohort_monthcohort_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(____, ____)
編輯並執行程式碼