w3resource

Pandas DataFrame: pivot() function

DataFrame - pivot() function

The pivot() function is used to reshaped a given DataFrame organized by given index / column values. This function does not support data aggregation, multiple values will result in a MultiIndex in the columns.

Syntax:

DataFrame.pivot(self, index=None, columns=None, values=None)

Parameters:

Name Description Type/Default Value Required / Optional
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: DataFrame
Returns reshaped DataFrame.

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

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - droplevel() function
Next: DataFrame - pivot_table() function



Share this Tutorial / Exercise on : Facebook and Twitter