0

There are 3 columns,

ACCOUNT, DATE, AMOUNT

I am trying to do the following in pandas, and plan to use the "sum" column after the group by output dataframe to do more works:

df_group_by=df.groupby('account').agg({'amount': [np.size, np.sum]},as_index=False).reset_index() # equal to "SELECT ACCOUNT, SUM(amount) as sum FROM table GROUP BY ACCOUNT;"

print (df_group['sum'])

and then I got the following error message:

KeyError: 'sum

I don't understand. Anyone could help? Thank you!

I am in python 3.6 & pandas 0.19.2

Chubaka
  • 2,933
  • 7
  • 43
  • 58

2 Answers2

0

df_group is a multi-index data frame, you can access the 'sum' column by df_group['amount']['sum']

0

This one works:

Pandas Groupby and Sum Only One Column

df_group_by=df.groupby(['account'], as_index=False)['amount'].sum()

print (df_group['amount'])
Chubaka
  • 2,933
  • 7
  • 43
  • 58