Is it possible to use in groupby a user-defined function that will be passed as arguments values of several columns, every column in a separate argument? In the following 'standard' example sum function is called onv1 and v2 columns separately:
In [110]: dct = {
...: 'id':[1,2,2,3,3,3],
...: 'vl':[1,1,1,1,1,1],
...: 'v2':[2,2,2,2,2,2]
...: }
...:
...: df = pd.DataFrame(dct)
...: df.groupby('id')['vl','v2'].sum()
...:
Out[110]:
vl v2
id
1 1 2
2 2 4
3 3 6
How to define mysum function with two arguments, with each argument to get it's own column something like:
def f(col1, col2):
return col1 * 2 + col2 * 3
So, in fact, this function merges two columns in one. Can this be done?