Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation over Partition - pandas Dataframe

Tags:

I am looking for the best way to aggregate values based on a particular partition , an equivalent of

SUM(TotalCost) OVER(PARTITION BY ShopName) Earnings  ( SQL server) 

I am able to do this by the following steps in Pandas , but looking for a native approach which I am sure should exist

TempDF= DF.groupby(by=['ShopName'])['TotalCost'].sum()  TempDF= TempDF.reset_index()   NewDF=pd.merge(DF , TempDF, how='inner', on='ShopName') 

Thanks a lot for reading through !

like image 873
Ivan KR Avatar asked Mar 10 '16 00:03

Ivan KR


People also ask

Can I use group by without aggregate function Pandas?

Instead of using groupby aggregation together, we can perform groupby without aggregation which is applicable to aggregate data separately.

How do Pandas use multiple aggregate functions?

To apply aggregations to multiple columns, just add additional key:value pairs to the dictionary. Applying multiple aggregation functions to a single column will result in a multiindex. Working with multi-indexed columns is a pain and I'd recommend flattening this after aggregating by renaming the new columns.

Is Pandas DataFrame homogeneous?

Python Pandas DataFrame is a heterogeneous two-dimensional object, that is, the data are of the same type within each column but it could be a different data type for each column and are implicitly or explicitly labelled with an index.


1 Answers

You can use pandas transform() method for within group aggregations like "OVER(partition by ...)" in SQL:

import pandas as pd import numpy as np  #create dataframe with sample data df = pd.DataFrame({'group':['A','A','A','B','B','B'],'value':[1,2,3,4,5,6]})  #calculate AVG(value) OVER (PARTITION BY group) df['mean_value'] = df.groupby('group').value.transform(np.mean)  df: group   value   mean_value A       1       2 A       2       2 A       3       2 B       4       5 B       5       5 B       6       5 
like image 135
Anton Kargapolov Avatar answered Jan 19 '23 18:01

Anton Kargapolov