Pandas crosstab, but with values from aggregation of third column
<p>Here is my problem:</p>

<pre><code>df = pd.DataFrame({'A': ['one', 'one', 'two', 'two', 'one'] ,
                   'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] ,
                   'C': [1, 0, 0, 1,0 ]})
</code></pre>

<p>I would like to generate something like output of <code>pd.crosstab</code> function, but values on the intersection of column and row should come from aggregation of third column:</p>

<pre><code>    Ar,  Br, Cr
one 0.5 0  0
two 1  0  0
</code></pre>

<p>For example, there are two cases of 'one' and 'Ar' corresponding values in column 'C' are 1,0 we sum up values in column 'C' (0+1) and divide by number of values   in column 'C', so we get (0+1)/2 =0.5. Whenever combination is not present we (like 'Cr' and 'one') we set it to zero. Any thoughts?</p>

<p>you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html" rel="nofollow">pivot_table()</a> method, which uses <code>aggfunc='mean'</code> per-default:</p>

<pre><code>In [46]: df.pivot_table(index='A', columns='B', values='C', fill_value=0)
Out[46]:
B     Ar  Br  Cr
A
one  0.5   0   0
two  1.0   0   0
</code></pre>

python pandas aggregate