pandas 1.4.2

NotesParametersReturnsBackRef
pivot_table(self, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=False, sort=True) -> 'DataFrame'

The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the result DataFrame.

Notes

Reference the user guide <reshaping.pivot> for more examples.

Parameters

values : column to aggregate, optional
index : column, Grouper, array, or list of the previous

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.

columns : column, Grouper, array, or list of the previous

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.

aggfunc : function, list of functions, dict, default numpy.mean

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.

fill_value : scalar, default None

Value to replace missing values with (in the resulting pivot table, after aggregation).

margins : bool, default False

Add all row / columns (e.g. for subtotal / grand totals).

dropna : bool, default True

Do not include columns whose entries are all NaN.

margins_name : str, default 'All'

Name of the row / column that will contain the totals when margins is True.

observed : bool, default False

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.

versionchanged
sort : bool, default True

Specifies if the result should be sorted.

versionadded

Returns

DataFrame

An Excel style pivot table.

Create a spreadsheet-style pivot table as a DataFrame.

See Also

DataFrame.melt

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

DataFrame.pivot

Pivot without aggregation that can handle non-numeric data.

wide_to_long

Wide panel to long format. Less flexible but more user-friendly than melt.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
...  "bar", "bar", "bar", "bar"],
...  "B": ["one", "one", "one", "two", "two",
...  "one", "one", "two", "two"],
...  "C": ["small", "large", "large", "small",
...  "small", "large", "small", "small",
...  "large"],
...  "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
...  "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
... df A B C D E 0 foo one small 1 2 1 foo one large 2 4 2 foo one large 2 5 3 foo two small 3 5 4 foo two small 3 6 5 bar one large 4 6 6 bar one small 5 8 7 bar two small 6 9 8 bar two large 7 9

This first example aggregates values by taking the sum.

This example is valid syntax, but we were not able to check execution
>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
...  columns=['C'], aggfunc=np.sum)
... table C large small A B bar one 4.0 5.0 two 7.0 6.0 foo one 4.0 1.0 two NaN 6.0

We can also fill missing values using the :None:None:`fill_value` parameter.

This example is valid syntax, but we were not able to check execution
>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
...  columns=['C'], aggfunc=np.sum, fill_value=0)
... table C large small A B bar one 4 5 two 7 6 foo one 4 1 two 0 6

The next example aggregates by taking the mean across multiple columns.

This example is valid syntax, but we were not able to check execution
>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
...  aggfunc={'D': np.mean,
...  'E': np.mean})
... table D E A C bar large 5.500000 7.500000 small 5.500000 8.500000 foo large 2.000000 4.500000 small 2.333333 4.333333

We can also calculate multiple types of aggregations for any given value column.

This example is valid syntax, but we were not able to check execution
>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
...  aggfunc={'D': np.mean,
...  'E': [min, max, np.mean]})
... table D E mean max mean min A C bar large 5.500000 9 7.500000 6 small 5.500000 9 8.500000 8 foo large 2.000000 5 4.500000 4 small 2.333333 6 4.333333 2
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.pivot pandas.core.frame.DataFrame.melt pandas.core.reshape.pivot.pivot pandas.core.frame.DataFrame.stack pandas.core.reshape.melt.wide_to_long

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#8021
type: <class 'function'>
Commit: