我有一个名为 Dataprep 的数据框,有 19 列和 18484 个原始数据。我用下面的代码聚合了我想要的列:
Dataprep.groupby('CustomerKey').OrderQuantity.agg('count')
CustomerKey 是客户的 ID 号,orderquantity 是每个客户的订单数量。数据看起来像
11000 - 1
11000 - 1
11000 - 1
11000 - 1
11001 - 1
11001 - 1
11001 - 1
11001 - 1
11002 - 1
11002 - 1
11002 - 1
11002 - 1
11002 - 1
11003 - 1
11003 - 1
11003 - 1
11003 - 1
11003 - 1
11004 - 1
11004 - 1
11004 - 1
11004 - 1
第一列是 customerkey,另一列是 orderquantity。
我的问题是如何将其应用于 DataFrame Dataprep。我想保留所有其他列。
谢谢
最佳答案
看来你需要 transform
:
Dataprep['new'] = Dataprep.groupby('CustomerKey').OrderQuantity.transform('count')
关于python - 如何将聚合和groupby应用于python中的DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46772164/