pandas 1.4.2

NotesParametersRaisesReturnsBackRef
pivot(self, index=None, columns=None, values=None) -> 'DataFrame'

Reshape data (produce a "pivot" table) based on column values. Uses unique values from specified :None:None:`index` / :None:None:`columns` to form axes of the resulting DataFrame. This function does not support data aggregation, multiple values will result in a MultiIndex in the columns. See the User Guide <reshaping> for more on reshaping.

Notes

For finer-tuned control, see hierarchical indexing documentation along with the related stack/unstack methods.

Reference the user guide <reshaping.pivot> for more examples.

Parameters

index : str or object or a list of str, optional

Column to use to make new frame's index. If None, uses existing index.

versionchanged

Also accept list of index names.

columns : str or object or a list of str

Column to use to make new frame's columns.

versionchanged

Also accept list of columns names.

values : str, object or a list of the previous, optional

Column(s) to use for populating new frame's values. If not specified, all remaining columns will be used and the result will have hierarchically indexed columns.

Raises

ValueError:

When there are any :None:None:`index`, :None:None:`columns` combinations with multiple values. DataFrame.pivot_table when you need to aggregate.

Returns

DataFrame

Returns reshaped DataFrame.

Return reshaped DataFrame organized by given index / column values.

See Also

DataFrame.pivot_table

Generalization of pivot that can handle duplicate values for one index/column pair.

DataFrame.unstack

Pivot based on the index values instead of a column.

wide_to_long

Wide panel to long format. Less flexible but more user-friendly than melt.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...  'two'],
...  'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...  'baz': [1, 2, 3, 4, 5, 6],
...  'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
... df foo bar baz zoo 0 one A 1 x 1 one B 2 y 2 one C 3 z 3 two A 4 q 4 two B 5 w 5 two C 6 t
This example is valid syntax, but we were not able to check execution
>>> df.pivot(index='foo', columns='bar', values='baz')
bar  A   B   C
foo
one  1   2   3
two  4   5   6
This example is valid syntax, but we were not able to check execution
>>> df.pivot(index='foo', columns='bar')['baz']
bar  A   B   C
foo
one  1   2   3
two  4   5   6
This example is valid syntax, but we were not able to check execution
>>> df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])
      baz       zoo
bar   A  B  C   A  B  C
foo
one   1  2  3   x  y  z
two   4  5  6   q  w  t

You could also assign a list of column names or a list of index names.

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({
...  "lev1": [1, 1, 1, 2, 2, 2],
...  "lev2": [1, 1, 2, 1, 1, 2],
...  "lev3": [1, 2, 1, 2, 1, 2],
...  "lev4": [1, 2, 3, 4, 5, 6],
...  "values": [0, 1, 2, 3, 4, 5]})
... df lev1 lev2 lev3 lev4 values 0 1 1 1 1 0 1 1 1 2 2 1 2 1 2 1 3 2 3 2 1 2 4 3 4 2 1 1 5 4 5 2 2 2 6 5
This example is valid syntax, but we were not able to check execution
>>> df.pivot(index="lev1", columns=["lev2", "lev3"],values="values")
lev2    1         2
lev3    1    2    1    2
lev1
1     0.0  1.0  2.0  NaN
2     4.0  3.0  NaN  5.0
This example is valid syntax, but we were not able to check execution
>>> df.pivot(index=["lev1", "lev2"], columns=["lev3"],values="values")
      lev3    1    2
lev1  lev2
   1     1  0.0  1.0
         2  2.0  NaN
   2     1  4.0  3.0
         2  NaN  5.0

A ValueError is raised if there are any duplicates.

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'],
...  "bar": ['A', 'A', 'B', 'C'],
...  "baz": [1, 2, 3, 4]})
... df foo bar baz 0 one A 1 1 one A 2 2 two B 3 3 two C 4

Notice that the first two rows are the same for our :None:None:`index` and :None:None:`columns` arguments.

This example is valid syntax, but we were not able to check execution
>>> df.pivot(index='foo', columns='bar', values='baz')
Traceback (most recent call last):
   ...
ValueError: Index contains duplicate entries, cannot reshape
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.pivot_table pandas.core.frame.DataFrame.melt pandas.core.frame.DataFrame.unstack pandas.core.frame.DataFrame.stack

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#7871
type: <class 'function'>
Commit: