如何在python中将聚合和groupby应用于数据帧?

如何在python中将聚合和groupby应用于数据帧?,python,pandas,Python,Pandas,我有一个名为Dataprep的数据框架,有19列和18484个raw。我用下面的代码聚合了我想要的列: 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 1

我有一个名为Dataprep的数据框架,有19列和18484个raw。我用下面的代码聚合了我想要的列:

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。我想保留所有其他列

谢谢

您似乎需要:


谢谢你的快速回复。是的,我有一个新专栏。它显示的是11000-4100-411000-411000-411001-411001-411001-411001-411001-4、11002-4、11002-411002-411002-4等。我正在寻找11000-411001-411002-4等。有什么办法可以得到吗?然后需要解决您的问题,为新列转换重复数据。当我将Dataprep与其他dataframe合并时,我得到了我想要的。感谢您迄今为止的帮助,jezreal.Done:-)稍后我将尝试解决如何将重复数据转换为新列的问题。
Dataprep['new'] = Dataprep.groupby('CustomerKey').OrderQuantity.transform('count')