问题:
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