简体   繁体   English

熊猫-首次满足条件时查找索引

[英]Pandas - Find the index for the first time a condition is true

I have data that looks something like this: 我的数据看起来像这样:

example = pd.Series([0,0,0,2,2,0], index = [2000, 2001, 2002, 2003, 2004, 2005])

How do I find the index corresponding to the first time that a condition is true? 如何找到与条件第一次成立相对应的索引?

For example, I want to find the index corresponding to the first nonzero entry, which in the example data is 2003. 例如,我要查找与第一个非零条目相对应的索引,在示例数据中该索引为2003。

This will do it: 这样做:

example.ne(0).idxmax()
#2003

By using nonzero 通过使用nonzero

example.index[example.nonzero()[0][0]]
Out[267]: 2003
example[example != 0].index[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM