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.
syntax:
pandas.pivot(data, index=None, columns=None, values=None)[source]
Parameters:
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.
Example:
Download the Pandas DataFrame Notebooks from here.
Previous: melt() function
Next: pivot_table() function
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework