Marknadskorgsanalys i Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Transaktion |
|---|---|
| 1 | biografi, historia |
| 2 | skönlitteratur |
| 3 | biografi, poesi |
| 4 | skönlitteratur, historia |
| 5 | biografi |
| ... | ... |
| 75000 | skönlitteratur, poesi |
Identifiera produkter som ofta köps tillsammans.
Bygg rekommendationer utifrån dessa fynd.
| TID | Transaktion |
|---|---|
| 11 | skönlitteratur, biografi |
| 12 | skönlitteratur, biografi |
| 13 | historia, biografi |
| ... | ... |
| 19 | skönlitteratur, biografi |
| 20 | skönlitteratur, biografi |
| ... | ... |
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
För en repetition, se 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

Marknadskorgsanalys i Python