Q:

Aggregation and grouped index with pandas

I have a dataframe like this:
User ID Year    Amount
A       2009    5
A       2010    6
A       2009    6
A       2010    1
B       2010    1
B       2009    1
B       2010    2
B       2010    5

And I want to sum of the user's amount for each year.. And want to result like this:
    Sum of amount   
User    2009    2010

  A        11   7
  B        1    8

I can handle this for only a year. Like this:
newdf= df.groupby('User ID')['Amount'].sum().reset_index()

A:

Use rename for new level of MultiIndex, then add column Year to groupby and unstack. Last for remove column names add rename_axis:
newdf = (df.rename(columns={'Amount':'Sum of amount'})
         .groupby(['User ID', 'Year'])
         .sum()
         .unstack()
         .rename_axis((None, None), axis=1))
print (newdf)

        Sum of amount     
                 2009 2010
User ID                   
A                  11    7
B                   1    8

But if not necessary rename column, solution is simplify:
newdf= df.groupby(['User ID', 'Year']).sum().unstack()
print (newdf)
        Amount     
Year      2009 2010
User ID            
A           11    7
B            1    8

