How to sum column section by date with Pandas?
How to sum column sections by date, for example in this case, first from 2000 to 2008 and then from 2009 to 2016?
Date Total Managed Expenditure
0 2001 503.2
1 2002 529.9
2 2003 559.8
3 2004 593.2
4 2005 629.5
5 2006 652.1
6 2007 664.3
7 2008 688.2
8 2009 732.0
9 2010 759.2
10 2011 769.2
11 2012 759.8
12 2013 760.6
13 2014 753.3
14 2015 757.6
15 2016 753.9
+3
source to share
2 answers
As pointed out in the comments, pandas.cut
for categorization, then groupby
:
Code:
df.groupby(pd.cut(df.Date, [2000, 2008, 2016]))['TME'].sum()
Test code:
df = pd.read_fwf(StringIO(
u"""
Date TME
2001 503.2
2002 529.9
2003 559.8
2004 593.2
2005 629.5
2006 652.1
2007 664.3
2008 688.2
2009 732.0
2010 759.2
2011 769.2
2012 759.8
2013 760.6
2014 753.3
2015 757.6
2016 753.9"""
), header=1)\
print(df.groupby(pd.cut(df.Date, [2000, 2008, 2016]))['TME'].sum())
Results:
Date
(2000, 2008] 4820.2
(2008, 2016] 6045.6
Name: TME, dtype: float64
+4
source to share