join/search/sum in Pandas Python
<p>i'm new to Panda and trying to learn it, I have a DataFrame in Panda with 3 different columns:</p>

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

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

<pre><code>   a             b         c
-----------------------------
'     Alice   5/5/2014     5 '
'     Bob     7/18/2014    1 '
'     Bob     8/10/2014    5 '
------------------------------  
</code></pre>

<p>what is the best way to do this in Panda.</p>

<p>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</p>

<p>The most efficient way is to first make sure your date column is of a <code>datetime</code> type:</p>

<pre><code>&gt;&gt;&gt; df2
       a          b  c
0  Alice   5/5/2014  2
1    Bob  7/18/2014  1
2  Alice   5/9/2014  3
3    Bob  8/10/2014  5
&gt;&gt;&gt; df2['b'] = pd.to_datetime(df2.b)
</code></pre>

<p>Then, index the <code>DataFrame</code> by the date column:</p>

<pre><code>&gt;&gt;&gt; df2.set_index('b',inplace=True)
&gt;&gt;&gt; df2
                a  c
b                   
2014-05-05  Alice  2
2014-07-18    Bob  1
2014-05-09  Alice  3
2014-08-10    Bob  5
</code></pre>

<p>Then use <code>groupby</code>:</p>

<pre><code>&gt;&gt;&gt; df2.groupby(['a',df2.index.month]).sum()
         c
a         
Alice 5  5
Bob   7  1
      8  5
&gt;&gt;&gt; 
</code></pre>

<p>And you can always go back to your original index:</p>

<pre><code>&gt;&gt;&gt; df2.reset_index(inplace=True)
&gt;&gt;&gt; df2
           b      a  c
0 2014-05-05  Alice  2
1 2014-07-18    Bob  1
2 2014-05-09  Alice  3
3 2014-08-10    Bob  5
</code></pre>

python pandas