@@ -4456,44 +4456,55 @@ def last_valid_index(self):
4456
4456
4457
4457
def pivot (self , index = None , columns = None , values = None ):
4458
4458
"""
4459
+ Return reshaped DataFrame organized by given index / column values.
4460
+
4459
4461
Reshape data (produce a "pivot" table) based on column values. Uses
4460
- unique values from index / columns to form axes of the resulting
4461
- DataFrame.
4462
+ unique values from specified `index` / `columns` to form axes of the resulting
4463
+ DataFrame. This function does not support data aggregation, multiple
4464
+ values will result in a MultiIndex in the columns. See the
4465
+ :ref:`User Guide <reshaping>` for more on reshaping.
4462
4466
4463
4467
Parameters
4464
4468
----------
4465
4469
index : string or object, optional
4466
- Column name to use to make new frame's index. If None, uses
4470
+ Column to use to make new frame's index. If None, uses
4467
4471
existing index.
4468
4472
columns : string or object
4469
- Column name to use to make new frame's columns
4473
+ Column to use to make new frame's columns.
4470
4474
values : string or object, optional
4471
- Column name to use for populating new frame's values. If not
4475
+ Column to use for populating new frame's values. If not
4472
4476
specified, all remaining columns will be used and the result will
4473
- have hierarchically indexed columns
4477
+ have hierarchically indexed columns.
4474
4478
4475
4479
Returns
4476
4480
-------
4477
- pivoted : DataFrame
4481
+ DataFrame
4482
+ Returns reshaped DataFrame.
4478
4483
4479
- See also
4484
+ Raises
4485
+ ------
4486
+ ValueError:
4487
+ When there are any `index`, `columns` combinations with multiple
4488
+ values. `DataFrame.pivot_table` when you need to aggregate.
4489
+
4490
+ See Also
4480
4491
--------
4481
4492
DataFrame.pivot_table : generalization of pivot that can handle
4482
- duplicate values for one index/column pair
4493
+ duplicate values for one index/column pair.
4483
4494
DataFrame.unstack : pivot based on the index values instead of a
4484
- column
4495
+ column.
4485
4496
4486
4497
Notes
4487
4498
-----
4488
4499
For finer-tuned control, see hierarchical indexing documentation along
4489
- with the related stack/unstack methods
4500
+ with the related stack/unstack methods.
4490
4501
4491
4502
Examples
4492
4503
--------
4493
-
4494
- >>> df = pd.DataFrame({'foo': ['one','one','one','two','two', 'two'],
4495
- 'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
4496
- 'baz': [1, 2, 3, 4, 5, 6]})
4504
+ >>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
4505
+ ... 'two'],
4506
+ ... 'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
4507
+ ... 'baz': [1, 2, 3, 4, 5, 6]})
4497
4508
>>> df
4498
4509
foo bar baz
4499
4510
0 one A 1
@@ -4504,16 +4515,36 @@ def pivot(self, index=None, columns=None, values=None):
4504
4515
5 two C 6
4505
4516
4506
4517
>>> df.pivot(index='foo', columns='bar', values='baz')
4507
- A B C
4518
+ bar A B C
4519
+ foo
4508
4520
one 1 2 3
4509
4521
two 4 5 6
4510
4522
4511
4523
>>> df.pivot(index='foo', columns='bar')['baz']
4512
- A B C
4524
+ bar A B C
4525
+ foo
4513
4526
one 1 2 3
4514
4527
two 4 5 6
4515
4528
4529
+ A ValueError is raised if there are any duplicates.
4530
+
4531
+ >>> df = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'],
4532
+ ... "bar": ['A', 'A', 'B', 'C'],
4533
+ ... "baz": [1, 2, 3, 4]})
4534
+ >>> df
4535
+ foo bar baz
4536
+ 0 one A 1
4537
+ 1 one A 2
4538
+ 2 two B 3
4539
+ 3 two C 4
4540
+
4541
+ Notice that the first two rows are the same for our `index`
4542
+ and `columns` arguments.
4516
4543
4544
+ >>> df.pivot(index='foo', columns='bar', values='baz')
4545
+ Traceback (most recent call last):
4546
+ ...
4547
+ ValueError: Index contains duplicate entries, cannot reshape
4517
4548
"""
4518
4549
from pandas .core .reshape .reshape import pivot
4519
4550
return pivot (self , index = index , columns = columns , values = values )
0 commit comments