Let me use this simple df as an example.
df = pd.DataFrame([[1,1,10],[1,1,30],[1,2,40],[2,3,50],[2,3,150],[2,4,100]],columns=["col_grp","col1","col2"])
col_grp col1 col2
0 1 1 10
1 1 1 30
2 1 2 40
3 2 3 50
4 2 3 150
5 2 4 100
I wish to groupby 'col_grp' and aggregate every other columns into a single value. For example, grp['col1'].apply(lambda x: x.sum()).
But instead of a simple 'sum' function f(x)=sum(x), I wish to compute a complicated function which takes not only on the pd.Series of 'col1' as its input, but also the grouping value, i.e., f(x,y). In this example, for the group with 'col_grp' value = 1, the y input is 1. The x input is the 'col1' Series [1,1,2] as usual. Assuming f(x,y) can be coded but its logic a bit complicated for other workaround methods, can I still utilize grp.apply(f)? If yes, how should I write the syntax. Thank you in advance.
I just found something like f(x1,x2) can be applied where x1 is the 'col1' series and x2 is the 'col2' series after grouping. f(x1,x2) returns a single value which is meant to be a statistic of each group. E.g.,
grp[['col1','col2']].apply(lambda x: x['col1'].sum() + x['col2'].median())
So this could be an effective way to solve my question. Just involve the grouping column into the x input. Like,
grp[['col_grp','col1','col2']].apply(lambda x: x['col_grp'].mean() + x['col1'].sum() + x['col2'].median())
And I can customize f(x) in anyway I like. I think I answered my own question. Welcome any further discussions:)