I want to apply multiple functions of multiple columns to a groupby object which results in a new pandas.DataFrame
.
I know how to do it in seperate steps:>
Another solid variation of the solution is to do what @MaxU did with this solution to a similar question and wrap the individual functions in a Pandas series, thus only requiring a reset_index()
to return a dataframe.
First, define the functions for transformations:
def ed(group):
return group.elapsed_time * group.num_cores).sum() / 86400
def rd(group):
return group.running_time * group.num_cores).sum() / 86400
Wrap them up in a Series using get_stats
:
def get_stats(group):
return pd.Series({'elapsed_days': ed(group),
'running_days':rd(group)})
Finally:
lasts.groupby('user').apply(get_stats).reset_index()