使用掩码选择掩码中的下一个值,如果它不匹配我的if条件检查



那里,我目前有一小段代码来确定一个变量是否会经常变化,是否在我的掩码数组中。如果它在掩码中,我希望将该变量分配给稍后使用,但如果它不在掩码中,我希望它能够在满足条件的掩码中选择下一个值。下面是我刚才的代码,输出:

import numpy as np
import pandas as pd

hyst = pd.DataFrame({"test":[12, 4, 5, 4, 1, 3, 2, 5, 10, 9, 7, 5, 3, 6, 3, 2 ,1, 5, 2]})
possible_overload_cycle = 1
index_pos_overload = np.array([hyst.index[i] for i in range(0, len(hyst)-1, 5)])
if possible_overload_cycle in index_pos_overload:
hyst_overload_cycle = possible_overload_cycle
else:
hyst_overload_cycle = index_pos_overload[np.nonzero(index_pos_overload)[0][0]]
print(hyst_overload_cycle)
5

对于可能的_overload_cycle小于5的值,这可以正常工作,但如果它是7,这是我的输出:


import numpy as np
import pandas as pd

hyst = pd.DataFrame({"test":[12, 4, 5, 4, 1, 3, 2, 5, 10, 9, 7, 5, 3, 6, 3, 2 ,1, 5, 2]})
possible_overload_cycle = 7
index_pos_overload = np.array([hyst.index[i] for i in range(0, len(hyst)-1, 5)])
if possible_overload_cycle in index_pos_overload:
hyst_overload_cycle = possible_overload_cycle
else:
hyst_overload_cycle = index_pos_overload[np.nonzero(index_pos_overload)[0][0]]
print(hyst_overload_cycle)
5

这不是我想要的输出,我想要的输出应该是:

print(hyst_overload_cycle)
10

我如何让它移动到蒙版中的下一个值?我用np。非零尝试,但我认为我需要改变第一个[0]来确定数组possible_overload_cycle的维度。任何帮助将非常感激!

这能解决问题吗?

index_pos_overload[list(index_pos_overload > possible_overload_cycle).index(True)]

最新更新