pyspark.pandas.Series.xs

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

返回序列的横截面。

此方法接受一个 参数,用于在MultiIndex的特定级别选择数据。

Parameters
key label or tuple of label

索引中包含的标签,或部分包含在MultiIndex中。

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

如果键部分包含在MultiIndex中,请指明使用哪些级别。级别可以通过标签或位置来引用。

Returns
Series

从原始系列中对应于所选索引级别的横截面。

示例

>>> 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

获取指定索引处的值

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

获取多个索引处的值

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

获取指定索引和级别的值

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