pyspark.pandas.DataFrame.pop

DataFrame.pop(item: Union[Any, Tuple[Any, …]]) → pyspark.pandas.frame.DataFrame

Return item and drop from frame. Raise KeyError if not found.

Parameters
itemstr

Label of column to be popped.

Returns
Series

Examples

>>> df = ps.DataFrame([('falcon', 'bird', 389.0),
...                    ('parrot', 'bird', 24.0),
...                    ('lion', 'mammal', 80.5),
...                    ('monkey','mammal', np.nan)],
...                   columns=('name', 'class', 'max_speed'))
>>> df
     name   class  max_speed
0  falcon    bird      389.0
1  parrot    bird       24.0
2    lion  mammal       80.5
3  monkey  mammal        NaN
>>> df.pop('class')
0      bird
1      bird
2    mammal
3    mammal
Name: class, dtype: object
>>> df
     name  max_speed
0  falcon      389.0
1  parrot       24.0
2    lion       80.5
3  monkey        NaN

Also support for MultiIndex

>>> df = ps.DataFrame([('falcon', 'bird', 389.0),
...                    ('parrot', 'bird', 24.0),
...                    ('lion', 'mammal', 80.5),
...                    ('monkey','mammal', np.nan)],
...                   columns=('name', 'class', 'max_speed'))
>>> columns = [('a', 'name'), ('a', 'class'), ('b', 'max_speed')]
>>> df.columns = pd.MultiIndex.from_tuples(columns)
>>> df
        a                 b
     name   class max_speed
0  falcon    bird     389.0
1  parrot    bird      24.0
2    lion  mammal      80.5
3  monkey  mammal       NaN
>>> df.pop('a')
     name   class
0  falcon    bird
1  parrot    bird
2    lion  mammal
3  monkey  mammal
>>> df
          b
  max_speed
0     389.0
1      24.0
2      80.5
3       NaN