gpt4 book ai didi

python - 使用 df.apply 或 Pandas 中的类似方法修改行值的函数

转载 作者:行者123 更新时间:2023-12-04 13:28:14 25 4
gpt4 key购买 nike

概览
我正在处理一个数据框,其中 df["Pivots"]只要之前由锯齿形指标识别出高点或低点,它就会在 1 和 -1 之间交替。
我正在尝试在数据帧上使用 Pandas 实现以下内容,并在 df["Pivots"] 时修改相关行之前(错误地)被分配了一个值 1这标志着一个高,但另一行实际上有更高的 High值(value)。
请参阅下面的屏幕截图,以直观地表示数据和所需的输出。
伪代码

  • 如果当前行在 df["Pivots"] 中有 -1
  • rows_between = index < 当前行和索引 > df["Pivots"] 中的最后一个枢轴值这将是一个 1
  • df.High在rows_between > df["Pivot Price"]在当前行中,actual_high 是 df[High].max()在行之间。
  • df["Pivots"] 中删除 1 & 值(value)来自 df["Pivot Price"]在当前行并将其添加到 df["Pivots"] & df["Pivot Price"]在 actual_high 的行中

  • 示例
    在这个例子中 df.High成行 2023-10-08实际_高且高于 df["Pivot Price"]在行 2023-09-24 .
    这是原始数据框。
    enter image description here
    这是 所需的输出 :
    enter image description here
    实际数据帧将包含许多行,这只是一个最小的、可重现的示例。
    代码
        df.to_dict()

    {'Open': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0427,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0586,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0314,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0669,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 1.0058,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9966},
    'High': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0621,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0609,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0714,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0679,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 1.0198,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9966},
    'Low': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0383,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0297,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0285,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.004,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0.9941,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.938},
    'Close': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0577,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 1.0297,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 1.0666,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 1.0053,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0.9988,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.9528},
    'Pivots': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): 0,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): 0,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): 0,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): 0,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): -1},
    'Pivot Price': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): 1.0621,
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): nan,
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): nan,
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): nan,
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): nan,
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): 0.938},
    'Date': {Timestamp('2023-09-24 00:00:00', freq='W-SUN'): Timestamp('2023-09-24 00:00:00'),
    Timestamp('2023-10-01 00:00:00', freq='W-SUN'): Timestamp('2023-10-01 00:00:00'),
    Timestamp('2023-10-08 00:00:00', freq='W-SUN'): Timestamp('2023-10-08 00:00:00'),
    Timestamp('2023-10-15 00:00:00', freq='W-SUN'): Timestamp('2023-10-15 00:00:00'),
    Timestamp('2023-10-22 00:00:00', freq='W-SUN'): Timestamp('2023-10-22 00:00:00'),
    Timestamp('2023-10-29 00:00:00', freq='W-SUN'): Timestamp('2023-10-29 00:00:00')}}
    供引用, this是生成这些枢轴的代码。

    最佳答案

    我想不出使用 .apply() 的简短解决方案,但通过一些辅助功能,您可以使用以下代码解决问题:

    import numpy as np

    def get_highs_idx(df):
    return df[df['Pivots'] == 1].index.tolist()

    def get_lows_idx(df):
    return df[df['Pivots'] == -1].index.tolist()

    def get_previous_high_idx(df, low_idx):
    highs_idx = get_highs_idx(df)
    for high_idx in reversed(highs_idx):
    if high_idx < low_idx:
    return high_idx
    return None

    def reset_pivot(df, old_high_idx, new_high_idx):
    df.loc[old_high_idx, 'Pivots'] = 0
    df.loc[old_high_idx, 'Pivot Price'] = np.nan
    df.loc[new_high_idx, 'Pivots'] = 1
    df.loc[new_high_idx, 'Pivot Price'] = df.loc[new_high_idx, 'High']

    def correct_highs(df):
    lows_idx = get_lows_idx(df)
    for low_idx in lows_idx:
    high_idx = get_previous_high_idx(df, low_idx)
    if high_idx is not None:
    new_high_idx = df.loc[high_idx:low_idx, 'High'].idxmax()
    if high_idx != new_high_idx:
    reset_pivot(df, high_idx, new_high_idx)

    correct_highs(df)
    代码可能会减少一点,但我认为这种方式更清晰。
    编辑
    根据您的评论,我也在下面添加了用于纠正低点的代码。

    def get_previous_low_idx(df, high_idx):
    lows_idx= get_lows_idx(df)
    for low_idx in reversed(lows_idx):
    if low_idx < high_idx:
    return low_idx
    return None

    def reset_low_pivot(df, old_low_idx, new_low_idx):
    df.loc[old_low_idx, 'Pivots'] = 0
    df.loc[old_low_idx, 'Pivot Price'] = np.nan
    df.loc[new_low_idx, 'Pivots'] = -1
    df.loc[new_low_idx, 'Pivot Price'] = df.loc[new_low_idx, 'Low']

    def correct_lows(df):
    highs_idx = get_highs_idx(df)
    for high_idx in highs_idx:
    low_idx = get_previous_low_idx(df, high_idx)
    if low_idx is not None:
    new_low_idx = df.loc[low_idx:high_idx, 'Low'].idxmin()
    if low_idx != new_low_idx:
    reset_low_pivot(df, low_idx, new_low_idx)



    correct_lows(df)

    我不想影响原始答案,但您可能想重命名 reset_pivotreset_high_pivot为了一致性。
    还可以添加一个高级功能:
    def correct_pivots(df):
    correct_highs(df)
    correct_lows(df)

    关于python - 使用 df.apply 或 Pandas 中的类似方法修改行值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66797797/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com