Skip to content

Commit c7d768c

Browse files
committed
+pd.DataFrame.pivot_table
1 parent 4a64c39 commit c7d768c

File tree

2 files changed

+89
-73
lines changed

2 files changed

+89
-73
lines changed

pandas/core/frame.py

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@
109109
versionadded_to_excel='',
110110
versionadded_melt='\n.. versionadded:: 0.20.0\n',
111111
other_melt='melt',
112-
versionadded_crosstab = '\n.. versionadded:: 0.20.0\n',
113-
other_crosstab = 'crosstab')
112+
versionadded_crosstab='\n.. versionadded:: 0.20.0\n',
113+
other_crosstab='crosstab',
114+
versionadded_pivot_table='\n.. versionadded:: 0.20.0\n',
115+
other_pivot_table='pivot_table')
114116

115117
_numeric_only_doc = """numeric_only : boolean, default None
116118
Include only float, int, boolean data. If None, will attempt to use
@@ -3935,6 +3937,85 @@ def pivot(self, index=None, columns=None, values=None):
39353937
from pandas.core.reshape import pivot
39363938
return pivot(self, index=index, columns=columns, values=values)
39373939

3940+
_shared_docs['pivot_table'] = """
3941+
Create a spreadsheet-style pivot table as a DataFrame. The levels in the
3942+
pivot table will be stored in MultiIndex objects (hierarchical indexes) on
3943+
the index and columns of the result DataFrame
3944+
3945+
%(versionadded_pivot_table)s
3946+
3947+
Parameters
3948+
----------
3949+
data : DataFrame
3950+
values : column to aggregate, optional
3951+
index : column, Grouper, array, or list of the previous
3952+
If an array is passed, it must be the same length as the data. The list
3953+
can contain any of the other types (except list).
3954+
Keys to group by on the pivot table index. If an array is passed, it
3955+
is being used as the same manner as column values.
3956+
columns : column, Grouper, array, or list of the previous
3957+
If an array is passed, it must be the same length as the data. The list
3958+
can contain any of the other types (except list).
3959+
Keys to group by on the pivot table column. If an array is passed, it
3960+
is being used as the same manner as column values.
3961+
aggfunc : function or list of functions, default numpy.mean
3962+
If list of functions passed, the resulting pivot table will have
3963+
hierarchical columns whose top level are the function names (inferred
3964+
from the function objects themselves)
3965+
fill_value : scalar, default None
3966+
Value to replace missing values with
3967+
margins : boolean, default False
3968+
Add all row / columns (e.g. for subtotal / grand totals)
3969+
dropna : boolean, default True
3970+
Do not include columns whose entries are all NaN
3971+
margins_name : string, default 'All'
3972+
Name of the row / column that will contain the totals
3973+
when margins is True.
3974+
3975+
Examples
3976+
--------
3977+
>>> df
3978+
A B C D
3979+
0 foo one small 1
3980+
1 foo one large 2
3981+
2 foo one large 2
3982+
3 foo two small 3
3983+
4 foo two small 3
3984+
5 bar one large 4
3985+
6 bar one small 5
3986+
7 bar two small 6
3987+
8 bar two large 7
3988+
3989+
>>> table = pivot_table(df, values='D', index=['A', 'B'],
3990+
... columns=['C'], aggfunc=np.sum)
3991+
>>> table
3992+
small large
3993+
foo one 1 4
3994+
two 6 NaN
3995+
bar one 5 4
3996+
two 6 7
3997+
3998+
Returns
3999+
-------
4000+
table : DataFrame
4001+
4002+
See also
4003+
--------
4004+
DataFrame.pivot : pivot without aggregation that can handle
4005+
non-numeric data
4006+
%(other_pivot_table)s
4007+
"""
4008+
4009+
@Appender(_shared_docs['pivot_table'] % _shared_doc_kwargs)
4010+
def pivot_table(self, values=None, index=None, columns=None,
4011+
aggfunc='mean', fill_value=None, margins=False,
4012+
dropna=True, margins_name='All'):
4013+
from pandas.tools.pivot import pivot_table
4014+
return pivot_table(self, values=values, index=index, columns=columns,
4015+
aggfunc=aggfunc, fill_value=fill_value,
4016+
margins=margins, dropna=dropna,
4017+
margins_name=margins_name)
4018+
39384019
def stack(self, level=-1, dropna=True):
39394020
"""
39404021
Pivot a level of the (possibly hierarchical) column labels, returning a

pandas/tools/pivot.py

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,16 @@
1212

1313
from pandas.core.frame import _shared_docs
1414
from pandas.util.decorators import Appender
15-
_shared_docs_kwargs = dict(versionadded_crosstab="",
16-
other_crosstab="DataFrame.crosstab")
15+
_shared_doc_kwargs = dict(versionadded_crosstab='',
16+
other_crosstab='DataFrame.crosstab',
17+
versionadded_pivot_table='',
18+
other_pivot_table='DataFrame.pivot_table')
1719

1820

21+
@Appender(_shared_docs['pivot_table'] % _shared_doc_kwargs)
1922
def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
2023
fill_value=None, margins=False, dropna=True,
2124
margins_name='All'):
22-
"""
23-
Create a spreadsheet-style pivot table as a DataFrame. The levels in the
24-
pivot table will be stored in MultiIndex objects (hierarchical indexes) on
25-
the index and columns of the result DataFrame
26-
27-
Parameters
28-
----------
29-
data : DataFrame
30-
values : column to aggregate, optional
31-
index : column, Grouper, array, or list of the previous
32-
If an array is passed, it must be the same length as the data. The list
33-
can contain any of the other types (except list).
34-
Keys to group by on the pivot table index. If an array is passed, it
35-
is being used as the same manner as column values.
36-
columns : column, Grouper, array, or list of the previous
37-
If an array is passed, it must be the same length as the data. The list
38-
can contain any of the other types (except list).
39-
Keys to group by on the pivot table column. If an array is passed, it
40-
is being used as the same manner as column values.
41-
aggfunc : function or list of functions, default numpy.mean
42-
If list of functions passed, the resulting pivot table will have
43-
hierarchical columns whose top level are the function names (inferred
44-
from the function objects themselves)
45-
fill_value : scalar, default None
46-
Value to replace missing values with
47-
margins : boolean, default False
48-
Add all row / columns (e.g. for subtotal / grand totals)
49-
dropna : boolean, default True
50-
Do not include columns whose entries are all NaN
51-
margins_name : string, default 'All'
52-
Name of the row / column that will contain the totals
53-
when margins is True.
54-
55-
Examples
56-
--------
57-
>>> df
58-
A B C D
59-
0 foo one small 1
60-
1 foo one large 2
61-
2 foo one large 2
62-
3 foo two small 3
63-
4 foo two small 3
64-
5 bar one large 4
65-
6 bar one small 5
66-
7 bar two small 6
67-
8 bar two large 7
68-
69-
>>> table = pivot_table(df, values='D', index=['A', 'B'],
70-
... columns=['C'], aggfunc=np.sum)
71-
>>> table
72-
small large
73-
foo one 1 4
74-
two 6 NaN
75-
bar one 5 4
76-
two 6 7
77-
78-
Returns
79-
-------
80-
table : DataFrame
81-
82-
See also
83-
--------
84-
DataFrame.pivot : pivot without aggregation that can handle
85-
non-numeric data
86-
"""
8725
index = _convert_by(index)
8826
columns = _convert_by(columns)
8927

@@ -183,9 +121,6 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
183121
return table
184122

185123

186-
DataFrame.pivot_table = pivot_table
187-
188-
189124
def _add_margins(table, data, values, rows, cols, aggfunc,
190125
margins_name='All'):
191126
if not isinstance(margins_name, compat.string_types):
@@ -386,7 +321,7 @@ def _convert_by(by):
386321
return by
387322

388323

389-
@Appender(_shared_docs['crosstab'] % _shared_docs_kwargs)
324+
@Appender(_shared_docs['crosstab'] % _shared_doc_kwargs)
390325
def crosstab(index, columns, values=None, rownames=None, colnames=None,
391326
aggfunc=None, margins=False, dropna=True, normalize=False):
392327
index = com._maybe_make_list(index)

0 commit comments

Comments
 (0)