LoslegenKostenlos loslegen

Assign daily acquisition cohort

As you have seen in the video, defining a cohort is the first step to cohort analysis. You will now create daily cohorts based on the day each customer has made their first transaction.

The data has been loaded as online DataFrame, you can now print its header with online.head() in the console.

Diese Übung ist Teil des Kurses

Customer Segmentation in Python

Kurs anzeigen

Anleitung zur Übung

  • Create a function by passing a datetime object extracting year, month and day from x.
  • Create a InvoiceDay column by passing the InvoiceDate columns and applying the get_day function.
  • Create a groupby object that groups CustomerID variable, and selects InvoiceDay for further calculations.
  • Create a CohortDay column by selecting the minimum InvoiceDay value.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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())
Code bearbeiten und ausführen