bigframes.pandas.DataFrame.pivot_table#

DataFrame.pivot_table(values: Hashable | Sequence[Hashable] | None = None, index: Hashable | Sequence[Hashable] | None = None, columns: Hashable | Sequence[Hashable] = None, aggfunc: str = 'mean', fill_value=None, margins: bool = False, dropna: bool = True, margins_name: Hashable = 'All', observed: bool = False, sort: bool = True) DataFrame[source]#

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.

Examples:

>>> import bigframes.pandas as bpd
>>> df = bpd.DataFrame({
...     'Product': ['Product A', 'Product B', 'Product A', 'Product B', 'Product A', 'Product B'],
...     'Region': ['East', 'West', 'East', 'West', 'West', 'East'],
...     'Sales': [100, 200, 150, 100, 200, 150],
...     'Rating': [3, 5, 4, 3, 3, 5]
... })
>>> df
     Product Region  Sales  Rating
0  Product A   East    100       3
1  Product B   West    200       5
2  Product A   East    150       4
3  Product B   West    100       3
4  Product A   West    200       3
5  Product B   East    150       5

[6 rows x 4 columns]

Using pivot_table with default aggfunc “mean”:

>>> pivot_table = df.pivot_table(
...     values=['Sales', 'Rating'],
...     index='Product',
...     columns='Region'
... )
>>> pivot_table
          Rating       Sales
Region      East West   East   West
Product
Product A    3.5  3.0  125.0  200.0
Product B    5.0  4.0  150.0  150.0

[2 rows x 4 columns]

Using pivot_table with specified aggfunc “max”:

>>> pivot_table = df.pivot_table(
...     values=['Sales', 'Rating'],
...     index='Product',
...     columns='Region',
...     aggfunc="max"
... )
>>> pivot_table
          Rating      Sales
Region      East West  East West
Product
Product A      4    3   150  200
Product B      5    5   150  200

[2 rows x 4 columns]
Parameters:
  • values (str, object or a list of the previous, optional) – 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.

  • index (str or object or a list of str, optional) – Column to use to make new frame’s index. If not given, uses existing index.

  • columns (str or object or a list of str) – Column to use to make new frame’s columns.

  • aggfunc (str, default "mean") – Aggregation function name to compute summary statistics (e.g., ‘sum’, ‘mean’).

  • fill_value (scalar, default None) – Value to replace missing values with (in the resulting pivot table, after aggregation).

Returns:

An Excel style pivot table.

Return type:

bigframes.pandas.DataFrame