简体   繁体   English

通过 function 迭代行运行 df

[英]Running a df through a function iterating rows

So I am trying execute a buy/sell action based on the 5 period moving average exceeding that of the 20 period moving average.因此,我正在尝试基于 5 周期移动平均线超过 20 周期移动平均线执行买入/卖出操作。 After receiving too many errors I decided to do it separately for each relevant df instead of writing a function for it.在收到太多错误后,我决定为每个相关的 df 单独执行此操作,而不是为其编写 function。 An image of the final product can be seen below (last column of the screenshot is what I was hoping to achieve using one of these two functions).最终产品的图像如下所示(屏幕截图的最后一列是我希望使用这两个功能之一实现的)。 When I played around with these two functions I also came across the error which was 'key is invalid' which I didn't know how to solve.当我使用这两个功能时,我还遇到了“密钥无效”的错误,我不知道如何解决。 Any information would be much appreciated任何信息将不胜感激

def action(df):
    for i in range(df.index[]):
        if df.loc[i,'PointMovingAverage5'] > df.loc[i,'PointMovingAverage20'] & df.loc[i-1,'PointMovingAverage5'] < df.loc[i-1,'PointMovingAverage20']:
            df.loc[i, 'BUY/SELL'] = 'BUY'
        elif df.loc[i,'PointMovingAverage5'] < df.loc[i,'PointMovingAverage20'] & df.loc[i-1,'PointMovingAverage5'] > df.loc[i-1,'PointMovingAverage20']:
            df.loc[i, 'BUY/SELL'] = 'SELL'
        else:
            df.loc[i, 'BUY/SELL'] = 'NaN'
     
CBA['BUY/SELL'] = CBA.apply(action, axis = 1)

ERROR: 'str' object cannot be interpreted as an integer

def Action(df):
    for label, row in df.iterrows():
        if (df[row,'PointMovingAverage5'] > df[row,'PointMovingAverage20']) & (df[row-1,'PointMovingAverage5'] < df[row-1,'PointMovingAverage20']):
            df.loc[label, 'BUY/SELL'] = 'BUY'
        elif (df[row,'PointMovingAverage5'] < df[row,'PointMovingAverage20']) & (df[row-1,'PointMovingAverage5'] > df[row-1,'PointMovingAverage20']):
            df.loc[label, 'BUY/SELL'] = 'SELL'
        else:
            df.loc[label, 'BUY/SELL'] = 'NO ACTION'
        return df
           
CBA = CBA.apply(Action, axis = 1) 

ERROR: 'Series' object has no attribute 'iterrows'

End Goal最终目标

The reason it's not working is that iterrows returns (index, row), so the row value is the actual row of the dataframe and label is the column index.它不起作用的原因是 iterrows 返回 (index, row),因此行值是 dataframe 的实际行,而 label 是列索引。 Also, the first index will be zero, so you can't subtract one from it and you need to account for that.此外,第一个索引将为零,因此您不能从中减去一个,您需要考虑这一点。 I created the function below fixing those issues.我在下面创建了 function 来解决这些问题。 Note that the function won't work if the averages go from negative to zero to positive or vice versa.请注意,如果 go 的平均值从负到零再到正,则 function 将不起作用,反之亦然。

def Action(df):
    df = df.copy()
    df['BUY/SELL'] = 'NO ACTION'
    for i, row in df.iterrows():
        if i == 0:
            if (row['PointMovingAverage5'] < row['PointMovingAverage20']) & (df.loc[i+1,'PointMovingAverage5'] > df.loc[i+1,'PointMovingAverage20']):
                df.loc[i, 'BUY/SELL'] = 'BUY'
            elif (row['PointMovingAverage5'] > row['PointMovingAverage20']) & (df.loc[i+1,'PointMovingAverage5'] < df.loc[i+1,'PointMovingAverage20']):
                df.loc[i, 'BUY/SELL'] = 'SELL'
            continue
        if (row['PointMovingAverage5'] > row['PointMovingAverage20']) & (df.loc[i-1,'PointMovingAverage5'] < df.loc[i-1,'PointMovingAverage20']):
            df.loc[i, 'BUY/SELL'] = 'BUY'
        elif (row['PointMovingAverage5'] < row['PointMovingAverage20']) & (df.loc[i-1,'PointMovingAverage5'] > df.loc[i-1,'PointMovingAverage20']):
            df.loc[i, 'BUY/SELL'] = 'SELL'
    return df.copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM