pandas.pivot#
- pandas.pivot(data,*,columns,index=<no_default>,values=<no_default>)[source]#
Return reshaped DataFrame organized by given index / column values.
Reshape data (produce a “pivot” table) based on column values. Usesunique values from specifiedindex /columns to form axes of theresulting DataFrame. This function does not support dataaggregation, multiple values will result in a MultiIndex in thecolumns. See theUser Guide for more on reshaping.
- Parameters:
- dataDataFrame
Input pandas DataFrame object.
- columnsHashable or a sequence of the previous
Column to use to make new frame’s columns.
- indexHashable or a sequence of the previous, optional
Column to use to make new frame’s index. If not given, uses existing index.
- valuesHashable or a sequence of the previous, optional
Column(s) to use for populating new frame’s values. If notspecified, all remaining columns will be used and the result willhave hierarchically indexed columns.
- Returns:
- DataFrame
Returns reshaped DataFrame.
- Raises:
- ValueError:
When there are anyindex,columns combinations with multiplevalues.DataFrame.pivot_table when you need to aggregate.
See also
DataFrame.pivot_tableGeneralization of pivot that can handle duplicate values for one index/column pair.
DataFrame.unstackPivot based on the index values instead of a column.
wide_to_longWide panel to long format. Less flexible but more user-friendly than melt.
Notes
For finer-tuned control, see hierarchical indexing documentation alongwith the related stack/unstack methods.
Referencethe user guide 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 zoo0 one A 1 x1 one B 2 y2 one C 3 z3 two A 4 q4 two B 5 w5 two C 6 t
>>>df.pivot(index="foo",columns="bar",values="baz")bar A B Cfooone 1 2 3two 4 5 6
>>>df.pivot(index="foo",columns="bar")["baz"]bar A B Cfooone 1 2 3two 4 5 6
>>>df.pivot(index="foo",columns="bar",values=["baz","zoo"]) baz zoobar A B C A B Cfooone 1 2 3 x y ztwo 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 values0 1 1 1 1 01 1 1 2 2 12 1 2 1 3 23 2 1 2 4 34 2 1 1 5 45 2 2 2 6 5
>>>df.pivot(index="lev1",columns=["lev2","lev3"],values="values")lev2 1 2lev3 1 2 1 2lev11 0.0 1.0 2.0 NaN2 4.0 3.0 NaN 5.0
>>>df.pivot(index=["lev1","lev2"],columns=["lev3"],values="values") lev3 1 2lev1 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.
>>>df=pd.DataFrame(...{..."foo":["one","one","two","two"],..."bar":["A","A","B","C"],..."baz":[1,2,3,4],...}...)>>>df foo bar baz0 one A 11 one A 22 two B 33 two C 4
Notice that the first two rows are the same for ourindexandcolumns arguments.
>>>df.pivot(index="foo",columns="bar",values="baz")Traceback (most recent call last):...ValueError:Index contains duplicate entries, cannot reshape