Faranak Faranak - 2 months ago 13
Python Question

join/search/sum in Panda Python

i'm new to Panda and trying to learn it, I have a DataFrame in Panda with 3 different columns:

a b c
-----------------------------
' Alice 5/5/2014 2 '
' Bob 7/18/2014 1 '
' Alice 5/5/2014 3 '
' Bob 8/10/2014 5 '
------------------------------


I want to sum up the 'C' columns for each person per month, so the desired result would be like :

a b c
-----------------------------
' Alice 5/5/2014 5 '
' Bob 7/18/2014 1 '
' Bob 8/10/2014 5 '
------------------------------


what is the best way to do this in Panda.

if my question is repeated please re-direct me to other question i couln't find it maybe because i wasn't sure what to look for. thank you

Answer

add a column specifying the month

df['month'] = df['b'].month   # assuming it's a datetime object

then groupby and sum

df.groupby(['a','month']).sum()