pyspark.pandas.Series.xs

Series.xs(key: Union[Any, Tuple[Any, …]], level: Optional[int] = None) → pyspark.pandas.series.Series[source]

Return cross-section from the Series.

This method takes a key argument to select data at a particular level of a MultiIndex.

Parameters
keylabel or tuple of label

Label contained in the index, or partially in a MultiIndex.

levelobject, defaults to first n levels (n=1 or len(key))

In case of a key partially contained in a MultiIndex, indicate which levels are used. Levels can be referred by label or position.

Returns
Series

Cross-section from the original Series corresponding to the selected index levels.

Examples

>>> midx = pd.MultiIndex([['a', 'b', 'c'],
...                       ['lama', 'cow', 'falcon'],
...                       ['speed', 'weight', 'length']],
...                      [[0, 0, 0, 1, 1, 1, 2, 2, 2],
...                       [0, 0, 0, 1, 1, 1, 2, 2, 2],
...                       [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> s = ps.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
...               index=midx)
>>> s
a  lama    speed      45.0
           weight    200.0
           length      1.2
b  cow     speed      30.0
           weight    250.0
           length      1.5
c  falcon  speed     320.0
           weight      1.0
           length      0.3
dtype: float64

Get values at specified index

>>> s.xs('a')
lama  speed      45.0
      weight    200.0
      length      1.2
dtype: float64

Get values at several indexes

>>> s.xs(('a', 'lama'))
speed      45.0
weight    200.0
length      1.2
dtype: float64

Get values at specified index and level

>>> s.xs('lama', level=1)
a  speed      45.0
   weight    200.0
   length      1.2
dtype: float64