Analiza coșului de cumpărături în Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Tranzacție |
|---|---|
| 1 | biografie, istorie |
| 2 | ficțiune |
| 3 | biografie, poezie |
| 4 | ficțiune, istorie |
| 5 | biografie |
| ... | ... |
| 75000 | ficțiune, poezie |
Identificarea produselor achiziționate frecvent împreună.
Formularea recomandărilor pe baza acestor constatări.
| TID | Tranzacție |
|---|---|
| 11 | ficțiune, biografie |
| 12 | ficțiune, biografie |
| 13 | istorie, biografie |
| ... | ... |
| 19 | ficțiune, biografie |
| 20 | ficțiune, biografie |
| ... | ... |
import pandas as pd
# Load transactions from pandas.
books = pd.read_csv("datasets/bookstore.csv")
# Print the header
print(books.head(2))
TID Transaction
0 biography, history
1 fiction
Pentru o recapitulare, consultați Pandas Cheat Sheet.
# Split transaction strings into lists.
transactions = books['Transaction'].apply(lambda t: t.split(','))
# Convert DataFrame into list of strings.
transactions = list(transactions)
# Print the first transaction.
print(transactions[0])
['biography', 'history']
# Count the number of transactions that contain biography and fiction.
transactions.count(['biography', 'fiction'])
218
# Count the number of transactions that contain fiction and poetry.
transactions.count(['fiction', 'poetry'])
5357

Analiza coșului de cumpărături în Python