Set the column name to apply the result to the group

This is a pretty trivial problem, but its launch of my OCD and I could not find a suitable solution in the last half hour.

For the background, I am looking to calculate a value (let's call it F) for each group in a DataFrame derived from different aggregated column metrics in an existing DataFrame.

Here is an example of a game I'm trying to make:

import pandas as pd import numpy as np df = pd.DataFrame({'A': ['X', 'Y', 'X', 'Y', 'Y', 'Y', 'Y', 'X', 'Y', 'X'], 'B': ['N', 'N', 'N', 'M', 'N', 'M', 'M', 'N', 'M', 'N'], 'C': [69, 83, 28, 25, 11, 31, 14, 37, 14, 0], 'D': [ 0.3, 0.1, 0.1, 0.8, 0.8, 0. , 0.8, 0.8, 0.1, 0.8], 'E': [11, 11, 12, 11, 11, 12, 12, 11, 12, 12] }) df_grp = df.groupby(['A','B']) df_grp.apply(lambda x: x['C'].sum() * x['D'].mean() / x['E'].max()) 

I would like to assign a name to the result of apply (or lambda ). Is there a way to do this without moving lambda to a named function or renaming a column after running the last row?

+5
source share
2 answers

You can convert series to dataframe using reset_index() and provide name='yout_col_name' - the name of the column corresponding to the values ​​of the Series

 (df_grp.apply(lambda x: x['C'].sum() * x['D'].mean() / x['E'].max()) .reset_index(name='your_col_name')) AB your_col_name 0 XN 5.583333 1 YM 2.975000 2 YN 3.845455 
+6
source

Let the lambda function return a new series:

 df_grp.apply(lambda x: pd.Series({'new_name': x['C'].sum() * x['D'].mean() / x['E'].max()})) new_name ABXN 5.583333 YM 2.975000 N 3.845455 
+9
source

All Articles