Python-IF语句错误:IF为True时,Dataframe新列未返回正确的值

时间:2020-06-10 15:04:32

标签: python if-statement

我有一个包含多个if语句的循环。如果满足第一条语句,我想返回我在行中编码的int或float乘以其中一列的份额数之间的乘积。我希望循环逐行运行。但是,我的IF语句无法正确读取,因为第一行项目应返回3,而不是30。请参见下图。谢谢,纪尧姆

    trading['position size'] = 0

    for idx, row in trading.iterrows():
        i = trading['Close Price']
        x = trading['20-day SMA']
        y = trading['200-day SMA']
        z = trading['num of shrs']
        if (i > x) and (i > y):
            trading['position size'] = (1*z)
        elif (i > x) and (i < y) or (i < x) and (i > y):
            trading['position size'] = (0.5*z)
        elif (i < x) and (i < y):
            trading['position size'] = (0.1*z)
        else:
            trading['position size'] = np.nan

enter image description here

0 个答案:

没有答案