pandas - Set column name for apply result over groupby -


this trivial problem, triggering ocd , haven't been able find suitable solution past half hour.

for background, i'm looking calculate value (let's call f) each group in dataframe derived different aggregated measures of columns in existing dataframe.

here's toy example of i'm trying do:

import pandas pd import numpy 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()) 

what i'd assign name result of apply (or lambda). there anyway without moving lambda named function or renaming column after running last line?

you convert series dataframe using reset_index() , provide name='yout_col_name' -- name of column corresponding series values

(df_grp.apply(lambda x: x['c'].sum() * x['d'].mean() / x['e'].max())       .reset_index(name='your_col_name'))      b  your_col_name 0  x  n   5.583333 1  y  m   2.975000 2  y  n   3.845455 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -