[英]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.