pyspark.pandas.Series.unstack#

Series.unstack(level=-1)[source]#

Unstack, a.k.a. pivot, Series with MultiIndex to produce DataFrame. The level involved will automatically get sorted.

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

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

Returns:
DataFrame

Unstacked Series.

Notes

Unlike pandas, pandas-on-Spark doesn’t check whether an index is duplicated or not because the checking of duplicated index requires scanning whole data which can be quite expensive.

Examples

>>> s = ps.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).sort_index()
     a  b
one  1  2
two  3  4
>>> s.unstack(level=0).sort_index()
   one  two
a    1    3
b    2    4