Pandas: Data Manipulation - pivot() function

pivot() function

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 for more on reshaping.


pandas.pivot(data, index=None, columns=None, values=None)[source]


Name Description Type Required / Optional
data DataFrame Required
index Column to use to make new frame’s index. If None, uses existing index. string or object Optional
columns Column to use to make new frame’s columns. string or object Required
values 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. string, object or a list of the previous Optional

Returns: Returns reshaped DataFrame.

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


Download the above Notebook from here.

Previous: melt() function
Next: pivot_table() function