Nat*_*Raw 2 python bigdata dataframe pandas dask
我正在尝试做的是快速复制熊猫的值计数+ idxmax函数,因为我有很多数据。这是一个示例数据框:
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
Run Code Online (Sandbox Code Playgroud)
在大熊猫中,您可以这样操作:
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
Run Code Online (Sandbox Code Playgroud)
但是,当您在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''
Run Code Online (Sandbox Code Playgroud)
我真正想要做的是能够在达斯克的多列groupby之后获取值以及它们的出现次数。任何其他解决方案都是可以接受的,我只想把工作做好!
value_counts数据框的dask 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
Run Code Online (Sandbox Code Playgroud)