We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d9fdd00 commit 5aa5b80Copy full SHA for 5aa5b80
pandas/tools/pivot.py
@@ -51,10 +51,22 @@ def pivot_table(data, values=None, rows=None, cols=None, aggfunc=np.mean,
51
cols = _convert_by(cols)
52
53
keys = rows + cols
54
+
55
+ values_passed = values is not None
56
+ if values_passed:
57
+ if isinstance(values, (list, tuple)):
58
+ values_multi = True
59
+ else:
60
+ values_multi = False
61
+ values = [values]
62
63
64
+ data = data[keys + values]
65
66
grouped = data.groupby(keys)
67
- if values is not None:
- grouped = grouped[values]
68
+ if values_passed and not values_multi:
69
+ grouped = grouped[values[0]]
70
71
agged = grouped.agg(aggfunc)
72
pandas/tools/tests/test_pivot.py
@@ -19,7 +19,8 @@ def setUp(self):
19
'dull', 'shiny', 'shiny', 'dull',
20
'shiny', 'shiny', 'shiny'],
21
'D' : np.random.randn(11),
22
- 'E' : np.random.randn(11)})
+ 'E' : np.random.randn(11),
23
+ 'F' : np.random.randn(11)})
24
25
def test_pivot_table(self):
26
rows = ['A', 'B']
@@ -46,6 +47,13 @@ def test_pivot_table_multiple(self):
46
47
expected = self.data.groupby(rows + [cols]).agg(np.mean).unstack()
48
assert_frame_equal(table, expected)
49
50
+ def test_pivot_multi_values(self):
+ result = pivot_table(self.data, values=['D', 'E'],
+ rows='A', cols=['B', 'C'])
+ expected = pivot_table(self.data.drop(['F'], axis=1),
+ assert_frame_equal(result, expected)
if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
0 commit comments