Análisis de cesta de la compra en Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Transacción |
|---|---|
| 1 | biografía, historia |
| 2 | ficción |
| 3 | biografía, poesía |
| 4 | ficción, historia |
| 5 | biografía |
| ... | ... |
| 75000 | ficción, poesía |
Identificar productos comprados juntos con frecuencia.
Crear recomendaciones basadas en esto.
| TID | Transacción |
|---|---|
| 11 | ficción, biografía |
| 12 | ficción, biografía |
| 13 | historia, biografía |
| ... | ... |
| 19 | ficción, biografía |
| 20 | ficción, biografía |
| ... | ... |
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
Para repasar, consulta la 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

Análisis de cesta de la compra en Python