2017-08-01 139 views
1

我想要做的是在dask中複製熊貓值計數+ idxmax函數,因爲我有很多數據。下面是一個例子數據框:Dask在Groupby上覆制Pandas值計數

partner_num cust_id item_id revw_ratg_num revw_dt item_qty 
0 100 01 5 05/30/2000 0 
0 100 03 5 05/30/2000 0 
0 100 02 5 05/30/2000 0 
1 200 13 4 04/01/2000 0 
1 200 14 5 04/01/2000 1 
2 200 22 2 04/01/2000 1 
3 200 37 3 04/01/2000 1 
9 300 92 1 03/24/2000 1 
9 300 93 1 03/24/2000 1 
9 300 94 1 03/24/2000 0 
9 300 99 1 03/24/2000 0 
6 300 91 2 03/24/2000 0 

>>>df.head() 
    partner_num cust_id item_id revw_ratg_num  revw_dt item_qty 
0   0  100  1    5 05/30/2000   0 
1   0  100  3    5 05/30/2000   0 
2   0  100  2    5 05/30/2000   0 
3   1  200  13    4 04/01/2000   0 
4   1  200  14    5 04/01/2000   1 

在熊貓,你可以做這樣的:

df = pd.read_csv("fake_data.txt", sep="\t") 
df.groupby(["cust_id"]).item_qty.value_counts() 

cust_id item_qty 
100  0   3 
200  1   3 
     0   1 
300  0   3 
     1   2 

然而,當你去做好DASK同樣的事情,失敗,拋出一個屬性錯誤

df1 = dd.read_csv("fake_data.txt", sep="\t") 
df1.groupby(["cust_id"]).item_qty.value_counts() 

Traceback (most recent call last): 
    File "<pyshell#14>", line 1, in <module> 
    df1.groupby(["cust_id"]).item_qty.value_counts() 
AttributeError: 'SeriesGroupBy' object has no attribute 'value_counts'' 

我真正希望能夠做到的是能夠在Dask中獲得多列組之後的值以及它們的出現次數。任何替代解決方案都可以接受,我只想完成工作!

回答

3

value_countsdask API中不直接支持數據幀。使用apply來達到你想要的效果。

請注意,支持value_counts作爲Series方法。

>>> df1.groupby(['cust_id']).item_qty.apply(lambda x: x.value_counts()).compute() 
cust_id 
100  0 3 
200  1 3 
     0 1 
300  0 3 
     1 2 
Name: item_qty, dtype: int64 
+0

哇謝謝你!我知道有些事情不被支持,我只是不知道我錯在哪裏。 +1並接受編輯:由於代表哈哈,無法使用+1 –