在pandas系列中查找元素的索引

问题描述 投票:98回答:8

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

即,我想要像:

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)

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

python pandas
8个回答
145
投票
>>> myseries[myseries == 7]
3    7
dtype: int64
>>> myseries[myseries == 7].index[0]
3

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


31
投票

转换为索引,您可以使用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

9
投票
In [92]: (myseries==7).argmax()
Out[92]: 3

如果您事先知道7,那么这是有效的。您可以使用(myseries == 7).any()进行检查

另一种方法(非常类似于第一个答案)也考虑了多个7(或没有)

In [122]: myseries = pd.Series([1,7,0,7,5], index=['a','b','c','d','e'])
In [123]: list(myseries[myseries==7].index)
Out[123]: ['b', 'd']

3
投票

另一种方法是这样做,虽然同样不满意的是:

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

list(s).index(7)

回报:3

使用我正在使用的当前数据集进行时间测试(考虑它是随机的):

[64]:    %timeit pd.Index(article_reference_df.asset_id).get_loc('100000003003614')
10000 loops, best of 3: 60.1 µs per loop

In [66]: %timeit article_reference_df.asset_id[article_reference_df.asset_id == '100000003003614'].index[0]
1000 loops, best of 3: 255 µs per loop


In [65]: %timeit list(article_reference_df.asset_id).index('100000003003614')
100000 loops, best of 3: 14.5 µs per loop

2
投票

如果你使用numpy,你可以得到一个你的值找到的索引数组:

import numpy as np
import pandas as pd
myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4])
np.where(myseries == 7)

这将返回一个包含索引数组的元素元组,其中7是串联值:

(array([3], dtype=int64),)

1
投票

你可以使用Series.idxmax()

>>> import pandas as pd
>>> myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4])
>>> myseries.idxmax()
3
>>> 

0
投票

参考文献Viktor Kerkez(2013年8月20日5:52)Jonathan Eunice(16年11月7日14:03)

>>> myseries[myseries == 7]
3    7
dtype: int64
>>> myseries[myseries == 7].index   # using index[0] specifies the output of the first occurrence only.  Using index without adding the element index will give you indexes all occurrences if the series had more then one 7 there.  It still presumes you know which number you are looking for.  
3 

0
投票

通常,您的价值出现在多个指数处:

>>> myseries = pd.Series([0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1])
>>> myseries.index[myseries == 1]
Int64Index([3, 4, 5, 6, 10, 11], dtype='int64')
© www.soinside.com 2019 - 2024. All rights reserved.