Azfar Faizan Azfar Faizan - 1 month ago 41
Python Question

SUMIF like functions in Pandas

I have a dataframe like the following:

Date Attribute1 Attribute2 Attribute3
6/2/2014 7 6 9
6/2/2014 10 9 7
6/2/2014 6 6 8
6/3/2014 6 9 5
6/3/2014 5 7 6
6/4/2014 9 7 5
6/5/2014 8 6 8
6/5/2014 7 10 8


I want to apply each column values on dates such that the output looks like the following:

Date Attribute1 Attribute2 Attribute3
6/2/2014 23 21 24
6/3/2014 11 16 11
6/4/2014 9 7 5
6/5/2014 15 16 16


I do not know what possible methods are in Pandas to do this operation. Please help. Thanks.

Answer

Use a combination of groupby and sum operator:

df.groupby('Date').sum()
Out[34]: 
          Attribute1  Attribute2  Attribute3
Date                                        
6/2/2014          23          21          24
6/3/2014          11          16          11
6/4/2014           9           7           5
6/5/2014          15          16          16