Get startedGet started for free

Define target variable

Here, you'll build a pandas pivot table with customers as rows, invoice months as columns, and number of invoice counts as values. You will use the last month's value as the target variable. The remaining variables can be used as the so-called lagged features in the model. You will not use them, but are highly encouraged to check if adding these variables will improve your model performance beyond what you'll see in the upcoming exercises.

The pandas and numpy libraries have been loaded as pd as np respectively. The online dataset has been imported for you.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise instructions

  • Build a pivot table using the pivot_table() function counting invoices.
  • Store November 2011 sales data column name as a list.
  • Store the target value as Y.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Build a pivot table counting invoices for each customer monthly
cust_month_tx = pd.___(data=online, values='___',
                               index=['___'], columns=['___'],
                               aggfunc=pd.Series.nunique, fill_value=0)

# Store November 2011 data column name as a list
target = ['2011-___']

# Store target value as `Y`
Y = cust_month_tx[___]
Edit and Run Code