专家
我遇到了熊猫问题..
不确定为什么不满足以下条件。
df = pd.read_csv(path + filename, index_col='Date', parse_dates=True)
for i in range(1, len(signal)):
if [(df['x'] < 2) & (df['y'] <= 0)]:
listLongShort1.append("A_Condition")
# The other way around
elif [(df['x'] > 3) & (df['y'] >= 1)]:
listLongShort1.append("B_Condition")
else:
listLongShort1.append("No Condition")
由于某些原因,它只是用“ A_Condition”打印填充列,否则看不到省略号。
您能告诉我我的代码有什么问题吗?
谢谢!!
答案 0 :(得分:1)
&
是按位运算符。
AND
是您要用来检查条件的运算符。
if ((df['x'] < 2) and (df['y'] <= 0)):
listLongShort1.append("A_Condition")
elif ((df['x'] > 3) and (df['y'] >= 1)):
listLongShort1.append("B_Condition")
else:
listLongShort1.append("No Condition")
答案 1 :(得分:0)
python中的if
条件是语句,因此用法是:
if expression-here :
# your body
示例:
if (df['x'] < 2) and (df['y'] <= 0):
# whatever you want to do.
根据您所做的操作,该表达式位于列表([]
)内,并且其值始终为True
或False
,因此列表内的表达式始终为布尔值因此,它始终是一个元素的列表,该元素是python True
条件下的if
。
也就是说:
if [False]: # always true
# always get's executed
或
if [True]: # always true
# always get's executed
仅当列表为空时才通过,即:
if []: # always false
# doesn't get executed
但是由于您的情况是不可能的,因此您将面临一个总是“真实”的情况。