简体   繁体   English

Pandas 列基于上一行多列标准

[英]Pandas column based on previous row multicolumn criteria

I know this has been asked a few times, but I cannot get to solve it.我知道这已被问过几次,但我无法解决它。 I'm terribly sorry.我非常抱歉。 I have a dataframe 'stock_df' with 1 column with 'RSI_14' values.我有一个 dataframe 'stock_df' ,其中 1 列具有 'RSI_14' 值。 I'm a newbie, so in order to make it easier I initialize 3 columns with == 0 for 3 states.我是新手,所以为了更容易,我用 == 0 为 3 个状态初始化 3 列。 This auxiliary columns are to explain better the question:这个辅助栏目是为了更好地解释这个问题:

  • More than 70 --> 'plus70' = 1超过 70 --> 'plus70' = 1
  • Less than 30 --> 'minus30' = 1小于 30 --> 'minus30' = 1
  • Between 70 and 30 --> 'between' = 1在 70 到 30 之间 --> '介于' = 1

The objective is to make a 'Signal' that is +1 when 'RSI_14' > 70 and -1 when 'RSI_14' < 30 (that's easy), but the tricky part is that when the state is 'between' 70 and 30 I need to put the former +1 or -1 state in 'Signal' , and keep that number until the next +1 or -1 change of state and keep with that on and on... This shouldn't be that difficult with.shift(1) or.diff(1), but I don't get it.目标是在 ' RSI_14 ' > 70 和-1当 'RSI_14' < 30(这很容易)时产生一个“信号” ,但棘手的部分是当 state 是“介于”70 和 30 之间时需要将前+1 或 -1 state 放在'Signal'中,并保持该数字直到 state 的下一个 +1 或 -1变化,并一直保持下去......这应该不难。 shift(1) 或 .diff(1),但我不明白。

This is the desired outcome: Outcome这是期望的结果:结果

I've tried np.where, but it's the last "stock_df['Signal'].shift(1)" that doesn't seem to work:我试过np.where,但它是最后一个似乎不起作用的“stock_df['Signal'].shift(1)”:

stock_df['Signal'] = np.where(stock_df['RSI_14'] > 70, 1, (np.where(stock_df['RSI_14'] < 30, -1, stock_df['Signal'].shift(1))))

I think the solution must be in "groupby" with "transform" but I've tried many different ways, but I'm quite clumsy... I really think is with groupby.我认为解决方案必须在“groupby”“transform”中,但我尝试了很多不同的方法,但我很笨拙......我真的认为是 groupby。 I've checked A LOT of answers here, but I don't get to solve it.我在这里检查了很多答案,但我无法解决它。 I'd really appreciate your help.我真的很感谢你的帮助。 Thanks谢谢

I don't think you need to create any additional columns to achieve this.我认为您不需要创建任何其他列来实现此目的。 Just use:只需使用:

condition_values = stock_df.RSI_14.values
signal = []
last = None
for item in condition_values:
    if item < 30:
        signal.append(-1)
        last = -1
    elif item > 70:
        signal.append(1)
        last = 1
    else:
        signal.append(last)

 df['signal'] = signal

Change the none in the beginning to your liking value.将开头的 none 更改为您喜欢的值。

The following code snippet could work.以下代码片段可以工作。 Assigning signal value to be equal to the previous row signal value (when between equals 1) should get the job done.将信号值分配为等于前一行信号值(之间等于 1 时)应该可以完成工作。

for i in range(len(stock_df)):
    if stock_df['between'][i] == 1:
        if i == 0:
            stock_df['Signal'][i] = 1
        else:
            stock_df['Signal'][i] = stock_df['Signal'][i - 1]

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

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