假设我有一个Pandas DataFrame:
x = pd.DataFrame(data=[5,4,3,2,1,0,1,2,3,4,5],columns=['value'])
x
Out[9]:
value
0 5
1 4
2 3
3 2
4 1
5 0
6 1
7 2
8 3
9 4
10 5
现在,我希望,给定索引,在x
中查找行,直到满足条件。例如,如果index = 2
:
x.loc[2]
Out[14]:
value 3
Name: 2, dtype: int64
现在我想,从那个index
,找到下一个n
行,其值大于某些threshold
。例如,如果threshold is 0
,结果应该是:
x
Out[9]:
value
2 3
3 2
4 1
5 0
我怎样才能做到这一点?
我试过了:
x.loc[2:x['value']>0,:]
但当然这不起作用,因为x['value']>0
返回一个布尔数组:
Out[20]:
0 True
1 True
2 True
3 True
4 True
5 False
6 True
7 True
8 True
9 True
10 True
Name: value, dtype: bool
使用idxmin
和切片
x.loc[2:x['value'].gt(0).idxmin(),:]
2 3
3 2
4 1
5 0
Name: value
编辑:
对于通用配方,请使用
index = 7
threshold = 2
x.loc[index:x.loc[index:,'value'].gt(threshold).idxmin(),:]
根据你在评论中的描述,似乎你想从index+1
而不是索引开始。所以,如果是这种情况,请使用
x.loc[index+1:x.loc[index+1:,'value'].gt(threshold).idxmin(),:]
你想过滤索引大于你的index=2
和x['value']>=threshold
,然后选择这些行的第一个n
,这可以用.head(n)
完成。
说:
idx = 2
threshold = 0
n = 4
x[(x.index>=idx) & (x['value']>=threshold)].head(n)
日期:
# value
# 2 3
# 3 2
# 4 1
# 5 0
编辑:更改为> =,并更新示例以匹配OP的示例。
编辑2由于OP的澄清:因为n
未知:
idx = 2
threshold = 0
x.loc[idx:(x['value']<=threshold).loc[x.index>=idx].idxmax()]
这是从起始idx
中选择的,在这种情况下是idx=2
,直到并包括不满足条件的第一行(在本例中为索引5
)。