L.G.
L.G.

Reputation: 71

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

Upvotes: 1

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862711

It seems you need transform:

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

Upvotes: 1

Related Questions