Repository: pandas-dev/pandas

Issue:
DOC: enhance pivot / reshape docs
nice questions and answers here: https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe by piRsquared.

Would be nice to update our offical docs with some of these examples / tip & tricks. Maybe even use the Q & A format.


Discussion:
I'll take a stab at this.
@davetlewis-van are you working on this? If not, I'd like to. Thanks.
@pdpark Hi. Yes, I've started on it. I'll finish up this week. Can I send it your way for review when I have a draft?
Sure, I'd be happy to review it. Maybe just submit a PR though and I'll review it in github as soon as I see it come up?
@davetlewis-van @pdpark are you all still working on this? Would love to take a stab as I'm in Pycon Sprints right now!
Hi Vincent,

Yes, go for it. I keep meaning to get back to this but can’t find the time.
:(

On Mon, May 14, 2018 at 8:18 AM Vincent La <notifications@github.com> wrote:

> @davetlewis-van <https://github.com/davetlewis-van> @pdpark
> <https://github.com/pdpark> are you all still working on this? Would love
> to take a stab as I'm in Pycon Sprints right now!
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/issues/19089#issuecomment-388855339>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AWZgkN65DcpOuYisG0HNFyhwzDf5mtIJks5tyaBZgaJpZM4RT9fl>
> .
>
-- 
Dave Lewis
Senior Technical Writer

*Active**State*
O. 778.786.1100 |E. davel@activestate.com |http://www.activestate.com


Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -5503,50 +5503,72 @@ def pivot(self, index=None, columns=None, values=None):
         ...                    "C": ["small", "large", "large", "small",
         ...                          "small", "large", "small", "small",
         ...                          "large"],
-        ...                    "D": [1, 2, 2, 3, 3, 4, 5, 6, 7]})
+        ...                    "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
+        ...                    "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
         >>> df
-             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
+             A    B      C  D  E
+        0  foo  one  small  1  2
+        1  foo  one  large  2  4
+        2  foo  one  large  2  5
+        3  foo  two  small  3  5
+        4  foo  two  small  3  6
+        5  bar  one  large  4  6
+        6  bar  one  small  5  8
+        7  bar  two  small  6  9
+        8  bar  two  large  7  9
+
+        This first example aggregates values by taking the sum.
 
         >>> table = pivot_table(df, values='D', index=['A', 'B'],
         ...                     columns=['C'], aggfunc=np.sum)
         >>> table
         C        large  small
         A   B
-        bar one    4.0    5.0
-            two    7.0    6.0
-        foo one    4.0    1.0
-            two    NaN    6.0
+        bar one      4      5
+            two      7      6
+        foo one      4      1
+            two    NaN      6
+
+        We can also fill missing values using the `fill_value` parameter.
 
         >>> table = pivot_table(df, values='D', index=['A', 'B'],
-        ...                     columns=['C'], aggfunc=np.sum)
+        ...                     columns=['C'], aggfunc=np.sum, fill_value=0)
         >>> table
         C        large  small
         A   B
-        bar one    4.0    5.0
-            two    7.0    6.0
-        foo one    4.0    1.0
-            two    NaN    6.0
+        bar one      4      5
+            two      7      6
+        foo one      4      1
+            two      0      6
+
+        The next example aggregates by taking the mean across multiple columns.
+
+        >>> table = pivot_table(df, values=['D', 'E'], index=['A', 'C'],
+        ...                     aggfunc={'D': np.mean,
+        ...                              'E': np.mean})
+        >>> table
+                          D         E
+                       mean      mean
+        A   C
+        bar large  5.500000  7.500000
+            small  5.500000  8.500000
+        foo large  2.000000  4.500000
+            small  2.333333  4.333333
+
+        We can also calculate multiple types of aggregations for any given
+        value column.
 
         >>> table = pivot_table(df, values=['D', 'E'], index=['A', 'C'],
         ...                     aggfunc={'D': np.mean,
         ...                              'E': [min, max, np.mean]})
         >>> table
                           D   E
-                       mean max median min
+                       mean max      mean min
         A   C
-        bar large  5.500000  16   14.5  13
-            small  5.500000  15   14.5  14
-        foo large  2.000000  10    9.5   9
-            small  2.333333  12   11.0   8
+        bar large  5.500000  9   7.500000   6
+            small  5.500000  9   8.500000   8
+        foo large  2.000000  5   4.500000   4
+            small  2.333333  6   4.333333   2
 
         Returns
         -------


Test patch: