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
Post a Comment