A model learns from a table with one row per thing it predicts, here one row per customer. The columns of that table are the features. customers.csv already has one row per customer, but orders.csv has about 1,500 order lines. The starting code cleans the customers, gives each order line a value (quantity times unit price) and sums the lines of each order into by_order. You boil those orders down to two new features per customer: the average order value and the share of cancelled orders.
From orders to customers: comparing a column with a value, like orders["status"] == "paid", gives back one True or False per row, and the mean of True/False is the share of True. groupby("customer_id")[[...]].mean() gives back one row per customer with the mean of each column you name. Setting .columns to a list renames the columns, in order, and merge(other, on="customer_id") gives back a table where each row also gets the columns of the matching row in other. For example, per category:
by_category = orders.groupby("category")[["quantity", "unit_price"]].mean()
by_category.columns = ["avg_quantity", "avg_price"]
orders = orders.merge(by_category, on="category")
X, y and the split: drop(columns=[...]) gives back the table without the columns you list: X keeps the features, and y is the label, bought_again. train_test_split(X, y, ...) from scikit-learn gives back four parts, X_train, X_test, y_train, y_test. test_size=0.25 keeps a quarter of the rows for the final test, and stratify=y keeps the same share of 1s in both parts.
When you're done, the check after your code prints:
city age newsletter visits orders spent days_since_last returned_item avg_order_value cancelled_share 0 Rome 52.0 yes 11 6 720 21 0 120.000 0.0 1 Madrid 41.0 yes 7 3 201 12 0 67.000 0.0 2 Madrid 51.0 no 5 3 257 1 0 85.667 0.0 Average order value: 70.24 cancelled share: 0.074 Rows: 300 train: 225 test: 75 Bought again in train: 0.596 in test: 0.6
Your task: add a True/False "cancelled" column to by_order, build per_customer with each customer's mean "value" and "cancelled", rename its columns to "avg_order_value" and "cancelled_share", and merge it into customers. Then make X (without "customer_id", "joined" and "bought_again"), y, and a stratified split with test_size=0.25 and random_state=0.
Assignment
Follow the Coding Tutorial to add the two order features to customers and split it into training and test rows.
Hint
Group by_order, not orders: it has one row per order, so an order with three lines counts once. Never put the label bought_again in X.
Free plan: 30 tutor messages. PRO is unlimited.
