Phân tích giỏ hàng trong Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Giao dịch |
|---|---|
| 1 | tiểu sử, lịch sử |
| 2 | tiểu thuyết |
| 3 | tiểu sử, thơ |
| 4 | tiểu thuyết, lịch sử |
| 5 | tiểu sử |
| ... | ... |
| 75000 | tiểu thuyết, thơ |
Xác định sản phẩm thường mua cùng nhau.
Đưa ra khuyến nghị từ kết quả.
| TID | Giao dịch |
|---|---|
| 11 | tiểu thuyết, tiểu sử |
| 12 | tiểu thuyết, tiểu sử |
| 13 | lịch sử, tiểu sử |
| ... | ... |
| 19 | tiểu thuyết, tiểu sử |
| 20 | tiểu thuyết, tiểu sử |
| ... | ... |
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
Để ôn lại, xem 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

Phân tích giỏ hàng trong Python