delta_list = ['2','3','4',nan]
lb = ['1','2','3','4']
ub = ['5','6','7','8']
condition:
flag_1 = [np.where((np.array(delta_list) > np.array(lb)) & (np.array(delta_list) < np.array(ub)),0,1)]
here: if nan
is being compared with any value it is returning 1
(obvious) but I want it to be like if the comparison is with nan
in the delta_list
, it should always return nan
instead of the flag_1 condition
. How do I achieve this? Should I make a new list and compare the index with delta_list and replace that index value with nan? Please help!!
Expected Output : flag_1 = ['1','1','1','1']
Desired Output : flag_1 =['1','1','1',nan]
nan
because delta_list 4th element is nan
and hence
[np.where((np.array(delta_list) > np.array(lb)) & (np.array(delta_list) < np.array(ub)),0,1)]
should NOT BE evaluated at all if nan is encountered in delta_list and it should simply return nan
in my flag_1
list
If numbers are less like 10
is possible compare strings, so use numpy.select
with new condition for compare by NaN
s:
delta_list = ['2','3','4',np.nan]
lb = ['1','2','3','4']
ub = ['5','6','7','8']
m1 = (np.array(delta_list) > np.array(lb)) & (np.array(delta_list) < np.array(ub))
m2 = pd.isna(delta_list)
flag_1 = np.select([m2, m1], [np.nan, '1'], default='0')
print (flag_1)
['1' '1' '1' 'nan']
If need avoid string nan
is possible use None
:
flag_1 = np.select([m2, m1], [None, '1'], default='0')
print (flag_1)
['1' '1' '1' None]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.