在熊猫系列中查找元素的索引

新手上路,请多包涵

我知道这是一个非常基本的问题,但出于某种原因我找不到答案。如何在 python pandas 中获取系列的某些元素的索引? (第一次出现就足够了)

即,我想要这样的东西:

 import pandas as pd
myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4])
print myseries.find(7) # should output 3

当然,可以用循环定义这样的方法:

 def find(s, el):
    for i in s.index:
        if s[i] == el:
            return i
    return None

print find(myseries, 7)

但我认为应该有更好的方法。在那儿?

原文由 sashkello 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 227
2 个回答
>>> myseries[myseries == 7]
3    7
dtype: int64
>>> myseries[myseries == 7].index[0]
3

虽然我承认应该有更好的方法来做到这一点,但这至少避免了迭代和循环遍历对象并将其移动到 C 级别。

原文由 Viktor Kerkez 发布,翻译遵循 CC BY-SA 3.0 许可协议

转换为索引,您可以使用 get_loc

 In [1]: myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4])

In [3]: Index(myseries).get_loc(7)
Out[3]: 3

In [4]: Index(myseries).get_loc(10)
KeyError: 10

重复处理

In [5]: Index([1,1,2,2,3,4]).get_loc(2)
Out[5]: slice(2, 4, None)

如果非连续返回,将返回一个布尔数组

In [6]: Index([1,1,2,1,3,2,4]).get_loc(2)
Out[6]: array([False, False,  True, False, False,  True, False], dtype=bool)

内部使用哈希表,速度如此之快

In [7]: s = Series(randint(0,10,10000))

In [9]: %timeit s[s == 5]
1000 loops, best of 3: 203 µs per loop

In [12]: i = Index(s)

In [13]: %timeit i.get_loc(5)
1000 loops, best of 3: 226 µs per loop

正如 Viktor 指出的那样,创建索引会产生一次性创建开销(当您实际对索引执行某些操作时会产生开销,例如 is_unique

 In [2]: s = Series(randint(0,10,10000))

In [3]: %timeit Index(s)
100000 loops, best of 3: 9.6 µs per loop

In [4]: %timeit Index(s).is_unique
10000 loops, best of 3: 140 µs per loop

原文由 Jeff 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题