Is There Complete Overlap Between `pd.pivot_table` and `pd.DataFrame.groupby` + `pd.DataFrame.unstack`?
<p>(Please note that there's a question <a href="http://stackoverflow.com/questions/34702815/pandas-group-by-and-pivot-table-difference">Pandas: group by and Pivot table difference</a>, but this question is different.)</p>

<p>Suppose you start with a DataFrame</p>

<pre><code>df = pd.DataFrame({'a': ['x'] * 2 + ['y'] * 2, 'b': [0, 1, 0, 1], 'val': range(4)})
&gt;&gt;&gt; df
Out[18]: 
   a  b  val
0  x  0    0
1  x  1    1
2  y  0    2
3  y  1    3
</code></pre>

<p>Now suppose you want to make the index <code>a</code>, the columns <code>b</code>, the values in a cell <code>val</code>, and specify what to do if there are two or more values in a resulting cell:</p>

<pre><code>b  0  1
a      
x  0  1
y  2  3
</code></pre>

<p>Then you can do this either through</p>

<pre><code>df.val.groupby([df.a, df.b]).sum().unstack()
</code></pre>

<p>or through</p>

<pre><code>pd.pivot_table(df, index='a', columns='b', values='val', aggfunc='sum')
</code></pre>

<p>So it seems to me that there's a simple correspondence between correspondence between the two (given one, you could almost write a script to transform it into the other). I also thought of more complex cases with hierarchical indices / columns, but I still see no difference.</p>

<p>Is there something I've missed? </p>

<ul>
<li><p>Are there operations that can be performed using one and not the other? </p></li>
<li><p>Ar there, perhaps, operations easier to perform using one over the other? </p></li>
<li><p>If not, why not deprecate <code>pivot_tale</code>? <code>groupby</code> seems much more general.</p></li>
</ul>

<p>If I understood the source code for <code>pivot_table(index, columns, values, aggfunc)</code> correctly it's tuned up equivalent for:</p>

<pre><code>df.groupby([index + columns]).agg(aggfunc).unstack(columns)
</code></pre>

<p><strong>plus:</strong></p>

<ul>
<li>margins (subtotals and grand totals as <a href="http://stackoverflow.com/questions/39674876/is-there-complete-overlap-between-pd-pivot-table-and-pd-dataframe-groupby#comment66651719_39674876">@ayhan has already said</a>)</li>
<li><code>pivot_table()</code> also removes extra multi-levels from columns axis (see example below)</li>
<li>convenient <code>dropna</code> parameter: Do not include columns whose entries are all NaN</li>
</ul>

<p>Demo: (I took this DF from the docstring [source code for <code>pivot_table()</code>])</p>

<pre><code>In [40]: df
Out[40]:
     A    B      C  D
0  foo  one  small  1
1  foo  one  large  2
2  foo  one  large  2
3  foo  two  small  3
4  foo  two  small  3
5  bar  one  large  4
6  bar  one  small  5
7  bar  two  small  6
8  bar  two  large  7

In [41]: df.pivot_table(index=['A','B'], columns='C', values='D', aggfunc=[np.sum,np.mean])
Out[41]:
          sum        mean
C       large small large small
A   B
bar one   4.0   5.0   4.0   5.0
    two   7.0   6.0   7.0   6.0
foo one   4.0   1.0   2.0   1.0
    two   NaN   6.0   NaN   3.0
</code></pre>

<p>pay attention at the top level column: <code>D</code></p>

<pre><code>In [42]: df.groupby(['A','B','C']).agg([np.sum, np.mean]).unstack('C')
Out[42]:
            D
          sum        mean
C       large small large small
A   B
bar one   4.0   5.0   4.0   5.0
    two   7.0   6.0   7.0   6.0
foo one   4.0   1.0   2.0   1.0
    two   NaN   6.0   NaN   3.0
</code></pre>

<blockquote>
  <p>why not deprecate pivot_tale? groupby seems much more general.</p>
</blockquote>

<p>IMO, because it's very easy to use and very convenient!
;)</p>

python pandas group-by pivot-table