| [ Web Proxy ] |
| Viewing: https://dataframes.bigquery.dev/reference/api/bigframes.pandas.DataFrame.pivot_table.html | [Back] [Original] |
Section Navigation
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]
values (str, object or a list of the previous, optional) Column(s) to use for populating new frames 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 frames index. If not given, uses existing index.
columns (str or object or a list of str) Column to use to make new frames 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).
An Excel style pivot table.
Copyright 2019, Google.
Created using Sphinx 8.1.3.
Built with the PyData Sphinx Theme 0.19.0.
| Web Proxy Viewer | New URL | Original Page |