在Python中,搜索值上升的4个连续行的位置

2024-10-03 11:12:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何标记价格属于4个涨价部分的行。 “是连续的”实际上是标记

我设法在行之间进行了区分:

df['diff1'] = df['Close'].diff()

但我没能找出哪一行是4次涨价的一部分

我想到了使用df.rolling()

例如df

在第0-3行上,我们需要在[“is_Concertive”]列上获得“True”的输出,因为此连续行上的['diff1']增加了4行

在第8-11行,我们需要在[“is_Concertive”]列上获得'False'的输出,因为这个连续行上的['diff1']为零

   Date      Price           diff1    is_consecutive   
0  1/22/20    0               0          True
1  1/23/20    130            130         True
2  1/24/20    144            14          True
3  1/25/20    150            6           True
4  1/27/20    60            -90          False
5  1/28/20    95             35          False
6  1/29/20    100            5           False
7  1/30/20    50            -50          False
8  2/01/20    100            0           False
9  1/02/20    100            0           False
10  1/03/20   100            0           False
11  1/04/20   100            0           False
12  1/05/20   50            -50          False

一般示例:

如果 价格=[30,55,60,65,25]

列表中连续数字的不同形式为:

diff1=[0,25,5,5,-40]

所以当diff1加上它实际上意味着连续的价格在上涨

我需要标记(在df中)有4个连续上升的行

感谢您的帮助(-:


Tags: 标记falsetruedfcloseisdiff价格
3条回答

尝试:.rolling,窗口大小为4,最小周期为1

df["is_consecutive"] = (
    df["Price"]
    .rolling(4, min_periods=1)
    .apply(lambda x: (x.diff().fillna(0) >= 0).all())
    .astype(bool)
)
print(df)

印刷品:

      Date  Price  is_consecutive
0  1/22/20      0            True
1  1/23/20    130            True
2  1/24/20    144            True
3  1/25/20    150            True
4  1/26/20     60           False
5  1/26/20     95           False
6  1/26/20    100           False
7  1/26/20     50           False

假设数据帧已排序。一种方法是基于差异的总和,以确定继3天上涨趋势(即4天上涨趋势)之后的首次上涨价格移动

quant1 = (df['Price'].diff().apply(np.sign) == 1).cumsum()
quant2 = (df['Price'].diff().apply(np.sign) == 1).cumsum().where(~(df['Price'].diff().apply(np.sign) == 1)).ffill().fillna(0).astype(int)
df['is_consecutive'] = (quant1-quant2) >= 3

请注意,以上仅考虑了严格上涨的价格(不相等)

然后,我们还使用win_view自定义函数覆盖前3个价格的is_Continuous标签,使其也TRUE

def win_view(x, size):
    if isinstance(x, list):
        x = np.array(x)
    if isinstance(x, pd.core.series.Series):
        x = x.values
    if isinstance(x, np.ndarray):
        pass
    else:
        raise Exception('wrong type')
    return np.lib.stride_tricks.as_strided(
        x,
        shape=(x.size - size + 1, size),
        strides=(x.strides[0], x.strides[0])
    )


arr = win_view(df['is_consecutive'], 4)
arr[arr[:,3]] = True

请注意,我们将替换为True的值

编辑1 受自定义的win_view函数的启发,我意识到,只需win_view(无需使用累积和)即可获得解决方案,如下所示:

df['is_consecutive'] = False
arr = win_view(df['Price'].diff(), 4)
arr_ind = win_view(list(df['Price'].index), 4)
mask = arr_ind[np.all(arr[:, 1:] > 0, axis=1)].flatten()
df.loc[mask, 'is_consecutive'] = True

我们维护2个数组,1个用于返回,1个用于索引。我们收集有3个连续正回报np.all(arr[:, 1:] > 0, axis=1(即4个上涨价格)的指数,并替换原始df中的指数

函数将返回名为"consecutive_up"的列,该列表示5个递增序列中的所有行;返回名为"consecutive_down"的列,该列表示4个法令序列中的所有行

def c_func(temp_df):

     temp_df['increase'] = temp_df['Price'] > temp_df['Price'].shift()
     temp_df['decrease'] = temp_df['Price'] < temp_df['Price'].shift()

     temp_df['consecutive_up'] = False
     temp_df['consecutive_down'] = False

     for ind, row in temp_df.iterrows():
          if row['increase'] == True:
               count += 1
          else:
               count = 0
          if count == 5:
               temp_df.iloc[ind - 5:ind + 1, 4] = True
          elif count > 5:
               temp_df.iloc[ind, 4] = True

     for ind, row in temp_df.iterrows():
          if row['decrease'] == True:
               count += 1
          else:
               count = 0
          if count == 4:
               temp_df.iloc[ind - 4:ind + 1, 5] = True
          elif count > 4:
               temp_df.iloc[ind, 5] = True
     return temp_df

相关问题 更多 >