[ad_1]
Could you please help me sort out the below if condition:
(could be also possible with apply and lambda but don’t know how to put so many conditions in single line)
test = pd.DataFrame({'index' : ['DS','VS','VB','FS','HB'],
'bid' : [np.nan,102,103,104,np.NaN],
'mid' : [106,107,108,109,110],
'ask' : [np.nan,112,113,114,115]})
print(test)
index bid mid ask
0 DS NaN 106 NaN
1 VS 102.0 107 112.0
2 VB 103.0 108 113.0
3 FS 104.0 109 114.0
4 HB NaN 110 115.0
What I tried :
if (test['index'].str.endswith('B') and test['bid'] > 0).all():
test['fin'] == test['bid']
elif (test['index'].str.endswith('S')) and (test['ask'] > 0).all():
test['fin'] == test['ask']
elif test['bid'] > 0:
test['fin'] == test['bid']
elif test['mid'] > 0:
test['fin'] == test['mid']
else:
test['fin'] == test['ask']
Still getting the error message:
ValueError: The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all()
.
[ad_2]