bigframes.pandas.DataFrame.stack#

DataFrame.stack(level: Hashable | Sequence[Hashable] = -1)[source]#

Stack the prescribed level(s) from columns to index.

Return a reshaped DataFrame or Series having a multi-level index with one or more new inner-most levels compared to the current DataFrame. The new inner-most levels are created by pivoting the columns of the current dataframe:

  • if the columns have a single level, the output is a Series;

  • if the columns have multiple levels, the new index

    level(s) is (are) taken from the prescribed level(s) and the output is a DataFrame.

Note

BigQuery DataFrames does not support stack operations that would combine columns of different dtypes.

Examples:

>>> df = bpd.DataFrame({'A': [1, 3], 'B': [2, 4]}, index=['foo', 'bar'])
>>> df
        A   B
foo     1   2
bar     3   4

[2 rows x 2 columns]
>>> df.stack()
foo  A    1
     B    2
bar  A    3
     B    4
dtype: Int64
Parameters:

level (int, str, or list of these, default -1 (last level)) – Level(s) to stack from the column axis onto the index axis.

Returns:

Stacked dataframe or series.

Return type:

bigframes.pandas.DataFrame or bigframes.pandas.Series