Skip to content

Commit a324d8d

Browse files
committed
REF: renamed xby/yby to rows/cols in pivot_table
1 parent 5a9dca0 commit a324d8d

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

doc/source/reshaping.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ tables. It takes a number of arguments
208208

209209
- ``data``: A DataFrame object
210210
- ``values``: column to aggregate
211-
- ``xby``: list of columns to group by on the `x`-axis
212-
- ``yby``: list of columns to group by on the `y`-axis
211+
- ``rows``: list of columns to group by on the table rows
212+
- ``cols``: list of columns to group by on the table columns
213213
- ``aggfunc``: function to use for aggregation, defaulting to ``numpy.mean``
214214

215215
Consider a data set like this:
@@ -227,8 +227,8 @@ We can produce pivot tables from this data very easily:
227227

228228
.. ipython:: python
229229
230-
pivot_table(df, values='D', xby=['A', 'B'], yby=['C'])
231-
pivot_table(df, values='D', xby=['B'], yby=['A', 'C'], aggfunc=np.sum)
230+
pivot_table(df, values='D', rows=['A', 'B'], cols=['C'])
231+
pivot_table(df, values='D', rows=['B'], cols=['A', 'C'], aggfunc=np.sum)
232232
233233
The result object is a DataFrame having potentially hierarchical indexes on the
234234
rows and columns. If the ``values`` column name is not given, the pivot table
@@ -237,12 +237,12 @@ hierarchy in the columns:
237237

238238
.. ipython:: python
239239
240-
pivot_table(df, xby=['A', 'B'], yby=['C'])
240+
pivot_table(df, rows=['A', 'B'], cols=['C'])
241241
242242
You can render a nice output of the table omitting the missing values by
243243
calling ``to_string`` if you wish:
244244

245245
.. ipython:: python
246246
247-
table = pivot_table(df, xby=['A', 'B'], yby=['C'])
247+
table = pivot_table(df, rows=['A', 'B'], cols=['C'])
248248
print table.to_string(na_rep='')

pandas/tools/pivot.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pandas import DataFrame
22
import numpy as np
33

4-
def pivot_table(data, values=None, xby=None, yby=None, aggfunc=np.mean,
4+
def pivot_table(data, values=None, rows=None, cols=None, aggfunc=np.mean,
55
fill_value=None):
66
"""
77
Create a spreadsheet-style pivot table as a DataFrame. The levels in the
@@ -12,9 +12,9 @@ def pivot_table(data, values=None, xby=None, yby=None, aggfunc=np.mean,
1212
----------
1313
data : DataFrame
1414
values : column to aggregate, optional
15-
xby : list
15+
rows : list
1616
Columns to group on the x-axis of the pivot table
17-
yby : list
17+
cols : list
1818
Columns to group on the x-axis of the pivot table
1919
aggfunc : function, default numpy.mean
2020
fill_value : scalar, default None
@@ -34,8 +34,8 @@ def pivot_table(data, values=None, xby=None, yby=None, aggfunc=np.mean,
3434
7 bar two small 6
3535
8 bar two large 7
3636
37-
>>> table = pivot_table(df, values='D', xby=['A, 'B'],
38-
yby=['C'], aggfunc=np.sum)
37+
>>> table = pivot_table(df, values='D', rows=['A, 'B'],
38+
cols=['C'], aggfunc=np.sum)
3939
>>> table
4040
small large
4141
foo one 1 4
@@ -47,10 +47,10 @@ def pivot_table(data, values=None, xby=None, yby=None, aggfunc=np.mean,
4747
-------
4848
table : DataFrame
4949
"""
50-
xby = _convert_by(xby)
51-
yby = _convert_by(yby)
50+
rows = _convert_by(rows)
51+
cols = _convert_by(cols)
5252

53-
keys = xby + yby
53+
keys = rows + cols
5454
grouped = data.groupby(keys)
5555

5656
if values is not None:
@@ -59,7 +59,7 @@ def pivot_table(data, values=None, xby=None, yby=None, aggfunc=np.mean,
5959
agged = grouped.agg(aggfunc)
6060

6161
table = agged
62-
for k in yby:
62+
for k in cols:
6363
table = table.unstack(level=k)
6464

6565
if fill_value is not None:
@@ -100,5 +100,5 @@ def _sample(values, n):
100100
data = DataFrame(data)
101101

102102
table = pivot_table(data, values='values',
103-
xby=['k1', 'k2'], yby=['k3', 'k4'])
103+
rows=['k1', 'k2'], cols=['k3', 'k4'])
104104

pandas/tools/tests/test_pivot.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ def setUp(self):
2222
'E' : np.random.randn(11)})
2323

2424
def test_pivot_table(self):
25-
xby = ['A', 'B']
26-
yby= 'C'
27-
table = pivot_table(self.data, values='D', xby=xby, yby=yby)
25+
rows = ['A', 'B']
26+
cols= 'C'
27+
table = pivot_table(self.data, values='D', rows=rows, cols=cols)
2828

29-
if len(xby) > 1:
30-
self.assertEqual(table.index.names, xby)
29+
if len(rows) > 1:
30+
self.assertEqual(table.index.names, rows)
3131
else:
32-
self.assertEqual(table.index.name, xby[0])
32+
self.assertEqual(table.index.name, rows[0])
3333

34-
if len(yby) > 1:
35-
self.assertEqual(table.columns.names, yby)
34+
if len(cols) > 1:
35+
self.assertEqual(table.columns.names, cols)
3636
else:
37-
self.assertEqual(table.columns.name, yby[0])
37+
self.assertEqual(table.columns.name, cols[0])
3838

39-
expected = self.data.groupby(xby + [yby])['D'].agg(np.mean).unstack()
39+
expected = self.data.groupby(rows + [cols])['D'].agg(np.mean).unstack()
4040
assert_frame_equal(table, expected)
4141

4242
def test_pivot_table_multiple(self):
43-
xby = ['A', 'B']
44-
yby= 'C'
45-
table = pivot_table(self.data, xby=xby, yby=yby)
46-
expected = self.data.groupby(xby + [yby]).agg(np.mean).unstack()
43+
rows = ['A', 'B']
44+
cols= 'C'
45+
table = pivot_table(self.data, rows=rows, cols=cols)
46+
expected = self.data.groupby(rows + [cols]).agg(np.mean).unstack()
4747
assert_frame_equal(table, expected)
4848

4949
if __name__ == '__main__':

0 commit comments

Comments
 (0)