bigframes.pandas.DataFrame.pivot#

DataFrame.pivot(*, columns: Hashable | Sequence[Hashable], index: Hashable | Sequence[Hashable] | None = None, values: Hashable | Sequence[Hashable] | None = None) DataFrame[source]#

Return reshaped DataFrame organized by given index / column values.

Reshape data (produce a “pivot” table) based on column values. Uses unique values from specified index / columns to form axes of the resulting DataFrame. This function does not support data aggregation, multiple values will result in a MultiIndex in the columns.

Note

BigQuery supports up to 10000 columns. Pivot operations on columns with too many unique values will fail if they would exceed this limit.

Note

The validity of the pivot operation is not checked. If columns and index do not together uniquely identify input rows, the output will be silently non-deterministic.

Examples:

>>> df = bpd.DataFrame({
...     "foo": ["one", "one", "one", "two", "two"],
...     "bar": ["A", "B", "C", "A", "B"],
...     "baz": [1, 2, 3, 4, 5],
...     "zoo": ['x', 'y', 'z', 'q', 'w']
... })
>>> df
    foo     bar     baz     zoo
0   one       A       1       x
1   one       B       2       y
2   one       C       3       z
3   two       A       4       q
4   two       B       5       w

[5 rows x 4 columns]

Using pivot without optional arguments:

>>> df.pivot(columns='foo')
        bar             baz             zoo
foo  one     two     one     two     one     two
0      A    <NA>       1    <NA>       x    <NA>
1      B    <NA>       2    <NA>       y    <NA>
2      C    <NA>       3    <NA>       z    <NA>
3   <NA>       A    <NA>       4    <NA>       q
4   <NA>       B    <NA>       5    <NA>       w

[5 rows x 6 columns]

Using pivot with index and values:

>>> df.pivot(columns='foo', index='bar', values='baz')
foo     one     two
bar
A       1         4
B       2         5
C       3      <NA>

[3 rows x 2 columns]
Parameters:
  • columns (str or object or a list of str) – Column to use to make new frame’s 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.

  • 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.

Returns:

Returns reshaped DataFrame.

Return type:

bigframes.pandas.DataFrame