w3resource

Pandas DataFrame: pivot_table() function

DataFrame - pivot_table() function

The pivot_table() function is used to 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:

DataFrame.pivot_table(self, 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
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 Value: numpy.mean
Required
fill_value  Value to replace missing values with scalar
Default Value: None
Required
margins  Add all row / columns (e.g. for subtotal / grand totals) boolean
Default Value: False
Required
dropna  Do not include columns whose entries are all NaN boolean
Default Value: True
Required
margins_name  Name of the row / column that will contain the totals when margins is True. string
Default Value: ‘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 Value: False
Required

Returns: DataFrame

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - pivot() function
Next: DataFrame - sort_values() function



Follow us on Facebook and Twitter for latest update.