by.html)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.pivot  #

DataFrame.  pivot  (  _ *  _ , _ columns  _ , _ index  =
_NoDefault.no_default  _ , _ values  =  _NoDefault.no_default  _ )  [ [source]
](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/frame.py#L9332-L9339) #

    

Return reshaped DataFrame organized by given index / column values.

Reshape data (produce a âpivotâ table) based on column values. Uses unique
values from specified  index  /  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
](../../user_guide/reshaping.html#reshaping) for more on reshaping.

Parameters  :

    

**columns** str or object or a list of str

    

Column to use to make new frameâs columns.

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

    

Column to use to make new frameâs index. If not given, uses existing index.

**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.

Returns  :

    

DataFrame

    

Returns reshaped DataFrame.

Raises  :

    

ValueError:

    

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

See also

[ ` DataFrame.pivot_table  `
](pandas.DataFrame.pivot_table.html#pandas.DataFrame.pivot_table
"pandas.DataFrame.pivot_table")

    

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

[ ` DataFrame.unstack  `
](pandas.DataFrame.unstack.html#pandas.DataFrame.unstack
"pandas.DataFrame.unstack")

    

Pivot based on the index values instead of a column.

[ ` wide_to_long  ` ](pandas.wide_to_long.html#pandas.wide_to_long
"pandas.wide_to_long")

    

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

Notes

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

Reference [ the user guide  ](../../user_guide/reshaping.html#reshaping-pivot)
for more examples.

Examples

    
    
    >>> 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
    
    
    
    >>> df.pivot(index='foo', columns='bar', values='baz')
    bar  A   B   C
    foo
    one  1   2   3
    two  4   5   6
    
    
    
    >>> df.pivot(index='foo', columns='bar')['baz']
    bar  A   B   C
    foo
    one  1   2   3
    two  4   5   6
    
    
    
    >>> 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.

    
    
    >>> 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