I am trying to group my data to years and sum the spending according to the year they belong to.
Here's a sample data:
date: spend_amt:
2/1/2014 10000
2/5/2014 98
1/2/2015 5834.2
7/8/2017 561236
9/3/2017 568
28/1/2016 989895.3
My current code
def yearlySpending(self):
dfspendingYearly = pd.DataFrame()
dfspendingYearly = self.dfGov.groupby(["date"])['spend_amt'].agg('sum')
dfspendingYearly.groupby(dfspendingYearly["date"].dt.year)['spend_amt'].agg(['sum'])
I got an error, 'KeyError: 'date''
Desired output
date: spend_amt:
2014 10098
2015 5834.2
2016 989895.3
2017 561804