2017-09-04 70 views
0

我有一個包含銷售數據的CSV文件,我希望通過該文件列出經常由同一買方購買的類別。我想我可以用字典和a script like this來做到這一點,但是我很難概念化如何計算同一買家在不同類別中出現的次數。計算出現特定行值的頻率並在Python中輸出組合

CSV數據樣本:

buyer_id | order_id | category 
1, 10, shoes 
1, 11, outerwear 
2, 12, beauty 
2, 13, shoes 
2, 14, outerwear 

在此示例中,我會想知道,鞋子,外套是一個組合中的至少2倍。

+0

你想知道每個類別在csv中重複多少次? –

+0

您正在尋找市場購物籃分析。 (谷歌它,讀了它。) – DyZ

+0

顯示所需的結果 – RomanPerekhrest

回答

1
import pandas as pd 
#Creating dataframe 
data = pd.DataFrame(
    {'Buyer_ID': [1,1,2,2,2,1], 
    'Order_ID': [10,11,12,13,14,15], 
    'Category':['shoes','outerwear','beauty','shoes','outerwear','shoes'] 
    }) 

data 
Out[]: 
    Buyer_ID Category Order_ID 
0   1  shoes  10 
1   1 outerwear  11 
2   2  beauty  12 
3   2  shoes  13 
4   2 outerwear  14 
5   1  shoes  15 

# Output: Same buyer and unique categories 
data.groupby(["Buyer_ID", "Category"]).size() 

# Buyer_ID:1 with two shoes entry is displayed only once (hence only unique categories are considered). 
Out[]: 
Buyer_ID Category 
1   outerwear 1 
      shoes  2 
2   beauty  1 
      outerwear 1 
      shoes  1 
dtype: int64 
相關問題