[英]If a value is between an interval, select the value on the same row in another column
我的数据框如下所示:
s gamma_star
0 0.000000 0.6261
1 0.000523 0.6262
2 0.000722 0.6263
3 0.000861 0.6267
4 0.000972 0.6269
5 0.001061 0.6260
6 0.001147 0.6263
7 0.001218 0.6261
我有一个值s = 0.000871
,我需要寻找的是属于这个s
的相关 gamma_star 。 在上面的例子中, s
在3 0.000861 0.6261
和4 0.000972 0.6261
那么它相关的gamma_star
是0.6267
! 我有点卡住了,不知道如何开始,有什么想法吗? 谢谢!
你可以这样做:
df.loc[(df.s > s).idxmax()-1, 'gamma_star']
# 0.6267
使用条件将指示满足条件的起点
(df.s > s)
0 False
1 False
2 False
3 False
4 True
5 True
6 True
7 True
Name: s, dtype: bool
通过使用idxmax()
我们可以找到区间的开始:
(df.s > s).idxmax()-1
# 3
如果你通过 s 值,你可以用循环来做。
for i in range(len(s)-1):
if given_value > s[i] and given_value < s[i+1]:
gamma_s_wanted = gamma_star[i]
break
else:
gamma_s_wanted = gamma_star[-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.