[英]Pandas - Find the index for the first time a condition is true
我的數據看起來像這樣:
example = pd.Series([0,0,0,2,2,0], index = [2000, 2001, 2002, 2003, 2004, 2005])
如何找到與條件第一次成立相對應的索引?
例如,我要查找與第一個非零條目相對應的索引,在示例數據中該索引為2003。
這樣做:
example.ne(0).idxmax()
#2003
通過使用nonzero
example.index[example.nonzero()[0][0]]
Out[267]: 2003
example[example != 0].index[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.