Adding Grouped Dataframes
<p>I have two dataframes. I like the to add add the values in the columns together if the grouping is the same. Doing this with a simple addition works great as long as both group values are in each table. If they are not, it returns <code>nan</code>. I am assuming because you can't add <code>nan</code> and an <code>int</code>, but not sure how to work around this.</p>

<pre><code>import pandas as pd
df = pd.DataFrame(data=[['A', 4],
                        ['A', 1],
                        ['B', 1],
                        ['B', 5]],
                   columns=['Person', 'Days'])

df1 = pd.DataFrame(data=[['A', 5],
                        ['A', 0],
                        ['C', 3],
                        ['C', 5]],
                   columns=['Person', 'Days'])

df['Days'] = df['Days'] &lt;= 3
df1['Days'] = df1['Days'] &lt;= 3

df = df.groupby('Person').agg(['count'])
df1 = df1.groupby('Person').agg(['count'])

print df + df1
</code></pre>

<p>Actual Output:</p>

<pre><code>        Days
       count
Person      
A          4
B        NaN
C        NaN
</code></pre>

<p>Desired Output:</p>

<pre><code>        Days
       count
Person      
A          4
B          2
C          2
</code></pre>

<p><strong>UPDATE:</strong></p>

<pre><code>In [40]: funcs = ['count','sum']

In [41]: df.groupby('Person').agg(funcs).add(df1.groupby('Person').agg(funcs), fill_value=0)
Out[41]:
        Days
       count sum
Person
A        4.0   2
B        2.0   1
C        2.0   1
</code></pre>

<p><strong>Old answer:</strong></p>

<pre><code>In [14]: df.groupby('Person').size().to_frame('count').add(
   ....:  df1.groupby('Person').size().to_frame('count'), fill_value=0)
Out[14]:
        count
Person
A         4.0
B         2.0
C         2.0
</code></pre>

<p>PS I worked with original DFs - i didn't execute this code :</p>

<pre><code>df = df.groupby('Person').agg(['count'])
df1 = df1.groupby('Person').agg(['count'])
</code></pre>

python python-2.7 pandas