Skip to main content
`Ctrl`+`K`

# pandas.Series.unstack

Series.unstack(level=-1, fill_value=None, sort=True)

  Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.

  This method pivots a level of the MultiIndex labels, reshaping the Series into a DataFrame where the unstacked level becomes the columns.

  Parameters:
    level int, str, or list of these, default last level

      Level(s) to unstack, can pass level name.

    fill_value scalar value, default None

      Value to use when replacing NaN values.

    sort bool, default True

      Sort the level(s) in the resulting MultiIndex columns. This also orders the rows of the result: sorted by the remaining levels if `True`, in order of first appearance if `False`.

  Returns:
    DataFrame

      Unstacked Series.

  See also

  `DataFrame.unstack`

    Pivot the MultiIndex of a DataFrame.

  Notes

  Reference the user guide for more examples.

  Examples

      >>> s = pd.Series(
      ...     [1, 2, 3, 4],
      ...     index=pd.MultiIndex.from_product([["one", "two"], ["a", "b"]]),
      ... )
      >>> s
      one  a    1
           b    2
      two  a    3
           b    4
      dtype: int64

      >>> s.unstack(level=-1)
           a  b
      one  1  2
      two  3  4

      >>> s.unstack(level=0)
         one  two
      a    1    3
      b    2    4