在熊貓系列中查找元素的索引 - Find element's index in pandas Series

問題:

I know this is a very basic question but for some reason I can't find an answer.我知道這是一個非常基本的問題,但由於某種原因我找不到答案。 How can I get the index of certain element of a Series in python pandas?如何在 python pandas 中獲取某個系列元素的索引? (first occurrence would suffice) (第一次出現就足夠了)

Ie, I'd like something like:即,我想要類似的東西:

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

Certainly, it is possible to define such a method with a loop:當然,可以用循環定義這樣的方法:

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

print find(myseries, 7)

but I assume there should be a better way.但我認爲應該有更好的方法。 Is there?有沒有?


解決方案:

參考一: https://en.stackoom.com/question/1Etqq
參考二: https://stackoom.com/question/1Etqq
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章