2016-09-24 46 views
0

我的數據幀(DF)看起來像這樣如何獲取密鑰的重複計數與大熊貓等聚集一起

Customer_number Store_number year month last_buying_date1 amount  
    1    20   2014 10  2015-10-07  100 
    1    20   2014 10  2015-10-09  200 
    2    20   2014 10  2015-10-20  100 
    2    10   2014 10  2015-10-13  500 

,我希望得到一個輸出這樣

year month sum_purchase count_purchases distinct customers 
2014 10  900   4     3 

怎麼辦我使用Agg和group by得到了這樣的輸出。目前我正在使用2步組,但努力爭取不同客戶。這是我的方法

#### Step 1 - Aggregating everything at customer_number, store_number level 
aggregations = { 
    'amount': 'sum',  
    'last_buying_date1': 'count', 
    } 
grouped_at_Cust = DF.groupby(['customer_number','store_number','month','year']).agg(aggregations).reset_index() 
grouped_at_Cust.columns = ['customer_number','store_number','month','year','total_purchase','num_purchase'] 


#### Step2 - Aggregating at year month level 


aggregations = { 
    'total_purchase': 'sum',  
    'num_purchase': 'sum', 
    size 
    } 

Monthly_customers =  grouped_at_Cust.groupby(['year','month']).agg(aggregations).reset_index() 
Monthly_customers.colums = ['year','month','sum_purchase','count_purchase','distinct_customers'] 

我的鬥爭是在第二步。我如何在第二個聚合步驟中包含大小?

回答

1

您可以使用groupby.agg並提供函數nunique以返回組中唯一CustomerIn的數量。

df_grp = df.groupby(['year', 'month'], as_index=False)         \ 
      .agg({'purchase_amt':['sum','count'], 'Customer_number':['nunique']}) 

df_grp.columns = map('_'.join, df_grp.columns.values) 

df_grp 

Image


櫃面,要執行groupby操作時,試圖將它們分組不同(省略某些列):

df_grp_1 = df.groupby(['year', 'month']).agg({'purchase_amt':['sum','count']})  

df_grp_2 = df.groupby(['Store_number', 'month', 'year'])['Customer_number'].agg('nunique') 

採取多指標列的第一級,其包含執行操作的agg

df_grp_1.columns = df_grp_1.columns.get_level_values(1) 

合併他們回到使用他們組列的交叉點:

df_grp = df_grp_1.reset_index().merge(df_grp_2.reset_index().drop(['Store_number'], 
             axis=1), on=['year', 'month'], how='outer') 

重命名列新:

d = {'sum': 'sum_purchase', 'count': 'count_purchase', 'nunique': 'distinct_customers'} 

df_grp.columns = [d.get(x, x) for x in df_grp.columns] 
df_grp 

Image

+0

感謝@Nickil。但我的客戶被定義爲customer_number和store_number的組合。如何將他們結合起來做nunique? – sourav

+0

是否將'purchase_amt'總和/計數計算爲不使用'store_number'作爲分組對象之一?如果是這種情況,你需要爲不同的選擇做兩次「groupby」。 *請參閱編輯* –

+0

請參閱更新示例(編輯問題)。客戶不僅僅是customer_number,而是customer_number和store_number的組合。所以,如果我可以連接customer_number和商店編號,並且使用'nunique'實現你的解決方案,那麼這將起作用。但是concat會導致其他問題。 – sourav