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


Python cudf.MultiIndex.get_loc用法及代碼示例

用法:

MultiIndex.get_loc(key, method=None, tolerance=None)

獲取標簽或標簽元組的位置。

該位置以整數/切片或布爾掩碼的形式返回。

參數

key標簽或標簽元組(每個級別一個)
methodNone

返回

locint,切片對象或布爾掩碼
  • 如果索引是唯一的,則搜索結果是唯一的,返回一個 int.
  • 如果 index 是單調的,則 index 作為切片對象返回。
  • 否則, cudf 會盡最大努力將搜索結果轉換為切片對象,如果不這樣做,將返回一個布爾掩碼。請注意,在某些情況下,這可能會偏離 Pandas 的行為。

例子

>>> import cudf
>>> mi = cudf.MultiIndex.from_tuples(
...     [('a', 'd'), ('b', 'e'), ('b', 'f')])
>>> mi.get_loc('b')
slice(1, 3, None)
>>> mi.get_loc(('b', 'e'))
1
>>> non_monotonic_non_unique_idx = cudf.MultiIndex.from_tuples(
...     [('c', 'd'), ('b', 'e'), ('a', 'f'), ('b', 'e')])
>>> non_monotonic_non_unique_idx.get_loc('b') # differ from pandas
slice(1, 4, 2)

Pandas 兼容性說明

多索引。get_loc

該函數的返回類型可能與 Pandas 提供的方法不同。如果索引既不是按字典順序排序也不是唯一的,則盡最大努力將找到的索引強製轉換為切片。例如:

>>> import pandas as pd
>>> import cudf
>>> x = pd.MultiIndex.from_tuples([
...     (2, 1, 1), (1, 2, 3), (1, 2, 1),
...     (1, 1, 1), (1, 1, 1), (2, 2, 1),
... ])
>>> x.get_loc(1)
array([False,  True,  True,  True,  True, False])
>>> cudf.from_pandas(x).get_loc(1)
slice(1, 5, 1)

相關用法


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