Analisi del carrello in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Transazione |
|---|---|
| 1 | biografia, storia |
| 2 | narrativa |
| 3 | biografia, poesia |
| 4 | narrativa, storia |
| 5 | biografia |
| ... | ... |
| 75000 | narrativa, poesia |
Individua i prodotti spesso acquistati insieme.
Crea raccomandazioni basate su questi risultati.
| TID | Transazione |
|---|---|
| 11 | narrativa, biografia |
| 12 | narrativa, biografia |
| 13 | storia, biografia |
| ... | ... |
| 19 | narrativa, biografia |
| 20 | narrativa, biografia |
| ... | ... |
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
Per un ripasso, vedi il 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

Analisi del carrello in Python