From 64c4d7f1d6b27f4b7fd452acb0f8ca4f333bc60d Mon Sep 17 00:00:00 2001 From: Stefan Simik Date: Sun, 10 Dec 2017 18:05:01 +0100 Subject: [PATCH 1/4] Issue #18712: Doc improved: pivot_table(..) & aggfunc parameter --- pandas/core/frame.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9ce6b6148be56..fd459e9af9803 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4413,10 +4413,12 @@ def pivot(self, index=None, columns=None, values=None): list can contain any of the other types (except list). Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values. - aggfunc : function or list of functions, default numpy.mean + aggfunc : function or list of functions or dict, default numpy.mean If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves) + If dict is passed, the key is column to aggregate and value is function + or list of functions fill_value : scalar, default None Value to replace missing values with margins : boolean, default False @@ -4460,6 +4462,29 @@ def pivot(self, index=None, columns=None, values=None): foo one 4.0 1.0 two NaN 6.0 + >>> table = pivot_table(df, values='D', index=['A', 'B'], + ... columns=['C'], aggfunc=np.sum) + >>> table + ... # doctest: +NORMALIZE_WHITESPACE + 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 + + >>> table = pivot_table(df, values=['D', 'E'], index=['A', 'C'], + ... aggfunc={'D': np.mean, 'E': [min, max, np.median]} ) + >>> table + ... # doctest: +NORMALIZE_WHITESPACE + D E + mean max median 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 + Returns ------- table : DataFrame From 1cc42d33dea032548ca6b2a7a7895b87fbff50ad Mon Sep 17 00:00:00 2001 From: Stefan Simik Date: Sun, 10 Dec 2017 18:18:52 +0100 Subject: [PATCH 2/4] Issue #18712: Formatting (PEP8 warnings) fixed --- pandas/core/frame.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fd459e9af9803..cd93cb444a11c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4417,8 +4417,8 @@ def pivot(self, index=None, columns=None, values=None): If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves) - If dict is passed, the key is column to aggregate and value is function - or list of functions + If dict is passed, the key is column to aggregate and value + is function or list of functions fill_value : scalar, default None Value to replace missing values with margins : boolean, default False @@ -4474,12 +4474,13 @@ def pivot(self, index=None, columns=None, values=None): two NaN 6.0 >>> table = pivot_table(df, values=['D', 'E'], index=['A', 'C'], - ... aggfunc={'D': np.mean, 'E': [min, max, np.median]} ) + ... aggfunc={'D': np.mean, + ... 'E': [min, max, np.mean]}) >>> table ... # doctest: +NORMALIZE_WHITESPACE - D E + D E mean max median min - A C + 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 From cad6572ee2c8ead16e35314a52a5b440b29cf4c2 Mon Sep 17 00:00:00 2001 From: Stefan Simik Date: Tue, 12 Dec 2017 11:58:09 +0100 Subject: [PATCH 3/4] Issue #18712: PR review - issues fixed. --- pandas/core/frame.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index cd93cb444a11c..5fda267c26b4e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4413,7 +4413,7 @@ def pivot(self, index=None, columns=None, values=None): list can contain any of the other types (except list). Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values. - aggfunc : function or list of functions or dict, default numpy.mean + aggfunc : function, list of functions, dict, default numpy.mean If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves) @@ -4454,7 +4454,6 @@ def pivot(self, index=None, columns=None, values=None): >>> table = pivot_table(df, values='D', index=['A', 'B'], ... columns=['C'], aggfunc=np.sum) >>> table - ... # doctest: +NORMALIZE_WHITESPACE C large small A B bar one 4.0 5.0 @@ -4465,7 +4464,6 @@ def pivot(self, index=None, columns=None, values=None): >>> table = pivot_table(df, values='D', index=['A', 'B'], ... columns=['C'], aggfunc=np.sum) >>> table - ... # doctest: +NORMALIZE_WHITESPACE C large small A B bar one 4.0 5.0 From 68c0ff2fb4c9b4e7b6c4906748a3891887ba6397 Mon Sep 17 00:00:00 2001 From: Stefan Simik Date: Tue, 12 Dec 2017 12:15:45 +0100 Subject: [PATCH 4/4] Issue #18712: PR review - issues fixed. --- pandas/core/frame.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5fda267c26b4e..5f323d0f040bc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4475,7 +4475,6 @@ def pivot(self, index=None, columns=None, values=None): ... aggfunc={'D': np.mean, ... 'E': [min, max, np.mean]}) >>> table - ... # doctest: +NORMALIZE_WHITESPACE D E mean max median min A C