bigframes.pandas.DataFrame.unstack#

DataFrame.unstack(level: Hashable | Sequence[Hashable] = -1, *, fill_value: Any = None)[source]#

Pivot a level of the (necessarily hierarchical) index labels.

Returns a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels.

If the index is not a MultiIndex, the output will be a Series (the analogue of stack when the columns are not a MultiIndex).

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.unstack()
A   foo    1
    bar    3
B   foo    2
    bar    4
dtype: Int64
>>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), ('two', 'a')])
>>> df = bpd.DataFrame({'val': [1, 2, 3]}, index=index)
>>> df.unstack(fill_value=0)
        val
level_1   a  b
level_0
one       1  2
two       3  0

[2 rows x 2 columns]
Parameters:
  • level (int, str, or list of these, default -1 (last level)) – Level(s) of index to unstack, can pass level name.

  • fill_value (scalar, default None) – Value to use to replace missing values.

Returns:

DataFrame or Series.

Return type:

bigframes.pandas.DataFrame or bigframes.pandas.Series