開始使用免費開始

以天數計算時間位移-第 2 部分

做得很好!現在我們有 6 個資料集,分別包含發票(Invoice)與同群(Cohort)日期的年、月、日數值──invoice_yearcohort_yearinvoice_monthcohort_monthinvoice_daycohort_day

在這個練習中,你會先分別計算發票日期與同群日期在「年」、「月」、「日」上的差值,接著再計算兩者之間的「總天數差」。這會是你的天數位移(days offset),我們會在下一個練習用它來視覺化客戶數量。online 資料已經載入,你可以呼叫 online.head() 把開頭幾列印在主控台。

本練習屬於課程

Python 的客群分群

檢視課程

練習說明

  • 找出發票時間與建立同群時間之間的年數差。
  • 找出發票時間與建立同群時間之間的月數差。
  • 找出發票時間與建立同群時間之間的天數差。
  • 計算 CohortIndex 的天數(假設 1 年為 365 天,1 個月為 30 天)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Calculate difference in years
years_diff = ____ - ____

# Calculate difference in months
months_diff = ____ - ____

# Calculate difference in days
days_diff = ____ - ____

# Extract the difference in days from all previous values
online['CohortIndex'] = years_diff * ____ + months_diff * ____ + days_diff + ____
print(online.head())
編輯並執行程式碼