當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark Series.xs用法及代碼示例

本文簡要介紹 pyspark.pandas.Series.xs 的用法。

用法:

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

從係列返回橫截麵。

此方法采用 key 參數來選擇 MultiIndex 特定級別的數據。

參數

key標簽或標簽元組

標簽包含在索引中,或部分包含在 MultiIndex 中。

level對象,默認為前 n 級(n=1 或 len(key))

如果鍵部分包含在 MultiIndex 中,請指明使用了哪些級別。級別可以通過標簽或位置來引用。

返回

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

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.pandas.Series.xs。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。