Pandas Index.get_loc(~) 方法返回源 Index 中给定值的位置。
参数
1. key | label
您想知道其位置的值。
2. method | None 或 string | optional
当源Index中不存在key时,如何处理这种情况:
| 
 方法  | 
 说明  | 
|---|---|
| 
 
  | 
 抛出一个错误。  | 
| 
 
  | 
 返回前一个索引值的整数索引。  | 
| 
 
  | 
 返回下一个索引值的整数索引。  | 
| 
 
  | 
 返回最近索引值的整数索引。 作为tie-breaker,较大的索引值将优先。  | 
默认情况下,method=None 。
警告
您的 Index 必须是单调的,否则指定 method 将引发错误。
3. tolerance | int 或 float | optional
key 和所选索引值之间允许的最大差异:
abs(index[loc] - key) <= tolerance
仅当方法不是 None 时,这才相关。默认情况下,不设置容差。
返回值
返回类型取决于以下情况:
| 
 案子  | 
 返回类型  | 
|---|---|
| 
 如果找到单个匹配项。  | 
 
  | 
| 
 如果找到多个匹配项并且 源   | 
 
  | 
| 
 Otherwise  | 
 布尔值的 Numpy 数组  | 
例子
单场比赛
要获取唯一值的整数索引:
idx = pd.Index(["a","b","c"])
idx.get_loc("c")
2
这里,值"c"的整数索引是2。
多场比赛
当索引单调时
要获取单调 Index 中不唯一的值的整数索引:
idx = pd.Index(["a","b","c","c"])
slice = idx.get_loc("c")
slice
slice(2, 4, None)
由于idx是单调递增的,因此这里的返回类型是slice对象。请注意,您可以像这样访问 slice 的属性:
print("start", slice.start)
print("stop", slice.stop)
print("step", slice.step)
start 2
stop 4
step None
当索引不单调时
要获取非单调 Index 中不唯一的值的位置:
idx = pd.Index(["a","b","c","b"])
idx.get_loc("b")
array([False,  True, False,  True])
此处,返回类型是布尔值的 Numpy 数组,其中 True 指示值的存在。
指定方法
默认情况下,当 Index 中不存在提供的值时,会抛出 KeyError :
idx = pd.Index([3,5,8])
idx.get_loc(6)
KeyError: 6
在这种情况下,当 Index 中不存在该值时,我们可以指定 method 来获取相邻值的整数索引。请注意,我们的Index必须是单调的,否则指定method将引发错误。
填充
当 method="ffill" 时,将返回前一个值的整数索引:
idx = pd.Index([3,5,8])
idx.get_loc(7, method="ffill")
1
这里,7的先前值是5,因此返回5的整数索引。
填充
当 method="bfill" 时,将返回下一个值的整数索引:
idx = pd.Index([3,5,8])
idx.get_loc(6, method="bfill")
2
这里,6的下一个值是8,因此返回8的整数索引。
最近的
当 method="nearest" 时,将返回最接近值的整数索引:
idx = pd.Index([3,5,8])
idx.get_loc(6, method="nearest")
1
这里,最接近 6 的值是 5 (与 8 相反),因此返回 5 的整数索引。
指定公差
如果未找到完全匹配,则设置 tolerance 会在提供的值和选定的索引值之间施加最大可接受的差异:
idx = pd.Index([3,5,8])
idx.get_loc(6, method="nearest", tolerance=0.5)
KeyError: 6
在这里,我们收到错误,因为最接近的索引值是 5 ,并且 5 和提供的值 6 之间的差异大于 0.5 的指定容差。
相关用法
- Python Pandas Index difference方法用法及代码示例
 - Python Django Index.expressions用法及代码示例
 - Python Django Index.include用法及代码示例
 - Python Int转Bytes用法及代码示例
 - Python Django InlineModelAdmin.get_extra用法及代码示例
 - Python Django InlineModelAdmin.raw_id_fields用法及代码示例
 - Python Pandas IntervalIndex构造函数用法及代码示例
 - Python InteractiveConsole runcode()用法及代码示例
 - Python InteractiveInterpreter runsource()用法及代码示例
 - Python InteractiveInterpreter runcode()用法及代码示例
 - Python IncrementalEncoder encode()用法及代码示例
 - Python Django InlineModelAdmin.get_max_num用法及代码示例
 - Python PIL Image.draft()用法及代码示例
 - Python PIL Image.thumbnail()用法及代码示例
 - Python PIL Image.new()用法及代码示例
 - Python PIL ImageOps.fit()用法及代码示例
 - Python Wand Image()用法及代码示例
 - Python PIL ImageDraw.Draw.rectangle()用法及代码示例
 - Python PIL ImageEnhance.Color() and ImageEnhance.Contrast()用法及代码示例
 - Python Itertools.zip_longest()用法及代码示例
 - Python PIL Image.getdata()用法及代码示例
 - Python Itertools.compress()用法及代码示例
 - Python PIL ImageFont.truetype()用法及代码示例
 - Python Itertools.count()用法及代码示例
 - Python PIL ImagePath.Path.tolist()用法及代码示例
 
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas Index | get_loc method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
