Uncovering Shopping Patterns in a German Retail Store using Association Rules

In the realm of retail analytics, understanding customer behavior is key to improving sales and customer satisfaction. One powerful tool for this task is association rule mining, which can reveal interesting patterns in customer purchasing habits. In this blog post, we’ll explore how association rules can be applied to transaction data from a German retail store.

Data Preparation

We start by importing the necessary libraries and loading the data from an Excel file. The dataset contains information about invoices, stock codes, descriptions, quantities, invoice dates, unit prices, customer IDs, and countries.

import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

data = pd.read_excel('data_path')

The dataset consists of 541,909 entries and 9 columns, with some missing values in the ‘Description’ and ‘CustomerID’ columns. We then clean the data by removing credit records and stripping whitespace from the ‘Description’ column.

Basket Creation

Next, we create a basket of items purchased by each customer, focusing on transactions from Germany.

basket_Germany = data[data['Country'] =="Germany"].groupby(['InvoiceNo', 'Description'])['Quantity'].sum().unstack().reset_index().fillna(0).set_index('InvoiceNo')
basket_encoded = basket_Germany.applymap(lambda x: 0 if x <=0 else 1) 
basket_Germany = basket_encoded

Association Rule Mining

We apply the Apriori algorithm to find frequent itemsets and then extract association rules based on these itemsets.

frq_items = apriori(basket_Germany, min_support = 0.05, use_colnames = True) 
rules = association_rules(frq_items, metric ="confidence", min_threshold = .1) 
rules = rules.sort_values(['confidence', 'lift'], ascending =[False, False]) 


The association rules reveal interesting insights into customer behavior. For example, we find that customers who purchase ‘JUMBO BAG WOODLAND ANIMALS’ are highly likely to also purchase ‘POSTAGE’, with a confidence of 1.0 and a lift of 1.31.


In conclusion, association rule mining can provide valuable insights into customer behavior and help businesses optimize their sales strategies. By understanding the relationships between different products, retailers can improve their marketing strategies and enhance customer satisfaction.


Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 7 =