DataFrame.pivot(index=None, columns=None, values=None)
[source]
Reshape data (produce a “pivot” table) based on column values. Uses unique values from index / columns to form axes of the resulting DataFrame.
Parameters: |
index : string or object, optional Column name to use to make new frame’s index. If None, uses existing index. columns : string or object Column name to use to make new frame’s columns values : string or object, optional Column name 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: |
pivoted : DataFrame |
See also
DataFrame.pivot_table
DataFrame.unstack
For finer-tuned control, see hierarchical indexing documentation along with the related stack/unstack methods
>>> df = pd.DataFrame({'foo': ['one','one','one','two','two','two'], 'bar': ['A', 'B', 'C', 'A', 'B', 'C'], 'baz': [1, 2, 3, 4, 5, 6]}) >>> df foo bar baz 0 one A 1 1 one B 2 2 one C 3 3 two A 4 4 two B 5 5 two C 6
>>> df.pivot(index='foo', columns='bar', values='baz') A B C one 1 2 3 two 4 5 6
>>> df.pivot(index='foo', columns='bar')['baz'] A B C one 1 2 3 two 4 5 6
© 2011–2012 Lambda Foundry, Inc. and PyData Development Team
© 2008–2011 AQR Capital Management, LLC
© 2008–2014 the pandas development team
Licensed under the 3-clause BSD License.
http://pandas.pydata.org/pandas-docs/version/0.19.2/generated/pandas.DataFrame.pivot.html