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


Python pandas.Index.get_loc用法及代碼示例

用法:

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

獲取請求標簽的整數位置、切片或布爾掩碼。

參數

key標簽
method{無,‘pad’/'ffill',‘backfill’/'bfill',‘nearest’},可選
  • 默認值:僅精確匹配。

  • pad /ffill:如果沒有完全匹配,則查找 PREVIOUS 索引值。

  • backfill /bfill:如果沒有完全匹配,則使用 NEXT 索引值

  • 最近:如果沒有完全匹配,則使用 NEAREST 索引值。通過首選較大的索引值來打破束縛距離。

toleranceint 或 float,可選

與不精確匹配的索引值的最大距離。匹配位置的索引值必須滿足等式 abs(index[loc] - key) <= tolerance

返回

loc如果是唯一索引則為 int,如果為單調索引則為 slice,否則為 mask

例子

>>> unique_index = pd.Index(list('abc'))
>>> unique_index.get_loc('b')
1
>>> monotonic_index = pd.Index(list('abbc'))
>>> monotonic_index.get_loc('b')
slice(1, 3, None)
>>> non_monotonic_index = pd.Index(list('abcb'))
>>> non_monotonic_index.get_loc('b')
array([False,  True, False,  True])

相關用法


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