Skip to content

Commit 44300f9

Browse files
amutaTomAugspurger
authored andcommitted
DOC: update the DataFrame.pivot docstring (#20250)
* DOC: DataFrame.pivot * DOC: grammar * Fixing pivot * Added Raises and example * Say what to do * Refer to user guide * Fixed name
1 parent 3392360 commit 44300f9

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

pandas/core/frame.py

+48-17
Original file line numberDiff line numberDiff line change
@@ -4456,44 +4456,55 @@ def last_valid_index(self):
44564456

44574457
def pivot(self, index=None, columns=None, values=None):
44584458
"""
4459+
Return reshaped DataFrame organized by given index / column values.
4460+
44594461
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.
44624466
44634467
Parameters
44644468
----------
44654469
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
44674471
existing index.
44684472
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.
44704474
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
44724476
specified, all remaining columns will be used and the result will
4473-
have hierarchically indexed columns
4477+
have hierarchically indexed columns.
44744478
44754479
Returns
44764480
-------
4477-
pivoted : DataFrame
4481+
DataFrame
4482+
Returns reshaped DataFrame.
44784483
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
44804491
--------
44814492
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.
44834494
DataFrame.unstack : pivot based on the index values instead of a
4484-
column
4495+
column.
44854496
44864497
Notes
44874498
-----
44884499
For finer-tuned control, see hierarchical indexing documentation along
4489-
with the related stack/unstack methods
4500+
with the related stack/unstack methods.
44904501
44914502
Examples
44924503
--------
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]})
44974508
>>> df
44984509
foo bar baz
44994510
0 one A 1
@@ -4504,16 +4515,36 @@ def pivot(self, index=None, columns=None, values=None):
45044515
5 two C 6
45054516
45064517
>>> df.pivot(index='foo', columns='bar', values='baz')
4507-
A B C
4518+
bar A B C
4519+
foo
45084520
one 1 2 3
45094521
two 4 5 6
45104522
45114523
>>> df.pivot(index='foo', columns='bar')['baz']
4512-
A B C
4524+
bar A B C
4525+
foo
45134526
one 1 2 3
45144527
two 4 5 6
45154528
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.
45164543
4544+
>>> df.pivot(index='foo', columns='bar', values='baz')
4545+
Traceback (most recent call last):
4546+
...
4547+
ValueError: Index contains duplicate entries, cannot reshape
45174548
"""
45184549
from pandas.core.reshape.reshape import pivot
45194550
return pivot(self, index=index, columns=columns, values=values)

0 commit comments

Comments
 (0)