python - Putting name to aggregated funtion in groupby pandas -


i add name sum column , call 'my_sum' [like "as" in sql sum(age) my_sum]

i managed this

my_group=df.groupby(['year']).agg({ 'age':{'my_sum': 'sum'}}) 

do think there easier way starting this?:

my_group=df.groupby(['year'])['age'].sum() 

i think can use reset_index parameter name:

my_group=df.groupby(['year'])['age'].sum().reset_index(name='my_sum') 

sample:

df = pd.dataframe({'year':[2000,2001,2000],                    'age':[40,50,60]}) print (df)    age  year 0   40  2000 1   50  2001 2   60  2000  my_group=df.groupby(['year'])['age'].sum().reset_index(name='my_sum') print (my_group)    year  my_sum 0  2000     100 1  2001      50 

Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -