简体   繁体   中英

How to apply aggregation and groupby to a DataFrame in python?

I have a dataframe called Dataprep with 19 columns and 18484 raws. I have aggregated the columns I wanted with the code below:

Dataprep.groupby('CustomerKey').OrderQuantity.agg('count')

CustomerKey is the ID number for customers and orderquantity is the number of orders by each customer. The data look like

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

First column is customerkey and other one is orderquantity.

My question is how would I apply this to the DataFrame Dataprep. I want to keep all other columns.

Thanks

看来你需要transform

Dataprep['new'] = Dataprep.groupby('CustomerKey').OrderQuantity.transform('count')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM