Pandas: Data Manipulation - pivot_table() function
pivot_table() function
Create a spreadsheet-style pivot table as a DataFrame.
Create a spreadsheet-style pivot table as a DataFrame. The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the result DataFrame.
syntax:
pandas.pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=False)
Parameters:
Name | Description | Type | Default Value | Required / Optional |
---|---|---|---|---|
data | DataFrame | Required | ||
values | column to aggregate | Optional | ||
index | If an array is passed, it must be the same length as the data. The list can contain any of the other types (except list). Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values. | column, Grouper, array, or list of the previous | Required | |
columns | If an array is passed, it must be the same length as the data. The list can contain any of the other types (except list). Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values. | column, Grouper, array, or list of the previous | Required | |
aggfunc | If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves) If dict is passed, the key is column to aggregate and value is function or list of functions | function, list of functions, dict | Default: numpy.mean | Required |
fill_value | Value to replace missing values with | scalar | Default: None | Required |
margins | Add all row / columns (e.g. for subtotal / grand totals) | boolean | Default: False | Required |
dropna | Do not include columns whose entries are all NaN | boolean | Default: True | Required |
margins_name | Name of the row / column that will contain the totals when margins is True. | string | Default: ‘All’ | Required |
observed | This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers. | boolean | Default: False | Required |
Returns: DataFrame.
Example:
Download the Pandas DataFrame Notebooks from here.
Previous: pivot() function
Next: crosstab() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/pivot_table.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics