【问题标题】:Python pandas how to deal with operationsPython pandas 如何处理操作
【发布时间】:2021-06-30 06:21:36
【问题描述】:

我有以下问题,我不确定如何以可接受的性能解决。

例子:

我有一个列表,其中包含 2 个日期之间股票的所有收盘价,比方说 close_price = [1, 2, 5, 3, 10, 1]

而且我也有一个只有 True 和 False 的数组,如果它是一个购买单,则它具有 True。 buy_ind = [False, True, False, False, True, False]

所有的滥用都在 pandas.DataFrame 中。

如果没有更好的 for 循环,我怎么能说这样的话?

我想检查 close_price 中在 buy_ind 中为 True 的所有元素(使用 np.where()),我想检查其中哪一个满足我的条件,然后转到下一个。

最后,我将得到一个包含所有卖出信号的数组。

谁能告诉我如何在不使用 for 循环的情况下解决这个问题,只遍历所有数组并检查条件?还是只有这样?

更新:

例子:

df['close'] = [1, 4, 5, 3, 1, 2]
df['buy'] = [False, True, False, False, True, False]
df['indicator'] = [3.1, 5.3, 0.3, 0.1, 6.2, 8.5, 9.0]
df['sell'] = [False,False,False,False,False,False]

我想要的是。

  1. 循环整体“收盘价”
  2. 在“购买”列中找到第一个为 True 的(例如,在我们的例子中是索引 1 中的值 4)
  3. 从同一索引上的“指标”列中获取值(在我们的例子中为 5.3)
  4. 检查以下哪个“收盘”价格将满足将使用 5.3 作为参数的某些条件
  5. 当你找到第一个满足条件的值时停止并将'sell'的索引设置为True

例如:如果我们从“收盘”价格 4 开始,相应地具有 5.3 值 indicator。我们将检查所有满足条件 (5.3) 的价格,例如指数 3 的“收盘”价格 3 是否满足 条件我们将设置索引为 'sell' True => [False,False,False,True,False,False]

  1. 转到下一个收盘价并重复该过程

【问题讨论】:

  • 我会制作一个数据框。 df 包含两列数据,然后使用 df['new column'] = df.apply(func) 创建第三列,其中 func 是一个函数,它采用两个原始列中的值的元组并返回您想要的任何内容。 apply 逐行应用你的函数
  • .apply(func) 逐列应用函数。 .apply(func, axis=1) 逐行应用函数。
  • 请添加输入数据帧的小样本以及预期数据帧的样子
  • @SamSzotkowski 和 @CameronRiddle 我不知道如何在我的情况下使用 apply
  • @tdy 我已经添加了一个小解释,如果不是很清楚,请告诉我,我会尝试编辑它。

标签: python pandas numpy numpy-ndarray


【解决方案1】:

我对你想要什么的理解:

对于“购买”列中为 True 的每一行:

  • 函数根据表格中该行下方所有“关闭”值的列表检查该行的指示器
  • 函数返回我应该卖出的行的索引

然后:

  • 更新函数返回其索引的所有行的“销售”

输入:

df = pd.DataFrame()
df['close'] = [1, 4, 5, 3, 1, 2]
df['buy'] = [False, True, False, False, True, False]
df['indicator'] = [3.1, 5.3, 0.3, 0.1, 6.2, 8.5]
df['sell'] = [False,False,False,False,False,False]
def conditions(index, indicator, close_list):
    compare = indicator * 10
    for close in close_list[index+1:]:
        if compare > close:
            return index
        index += 1
    return -1

close_list = df['close'].tolist()
df['row_index'] = df.index

get_conds = lambda row: conditions(row['row_index'],row['indicator'],close_list) \
                        if row['buy'] \
                        else -1
df['sell_index'] = df.apply(get_conds, axis=1)
sell_inds = df['sell_index'].dropna().tolist()

df['sell'] = [ind in sell_inds for ind in df['row_index']]

>>> print(df)

   close    buy  indicator   sell  row_index  sell_index
0      1  False        3.1  False          0          -1
1      4   True        5.3  False          1           2
2      5  False        0.3   True          2          -1
3      3  False        0.1  False          3          -1
4      1   True        6.2  False          4           5
5      2  False        8.5   True          5          -1

显然你可以删除中间步骤列(row_index 和 sell_index)

【讨论】:

  • 感谢您的回答,但这并不是我所需要的。让我试着用其他语言来解释。我有一份“买入”清单,它会告诉我,它的真实之处在哪里,这意味着它是买入股票的好时机。但后来我需要卖掉它以获取利润,我的条件在哪里到位。我想检查下一个满足我条件的“收盘价”,如果是,我会将这个值设置为 true。
  • buy = [False, True, False, False, False] close = [1, 2, 3, 1, 5, 6] indicator = [0.2, 0.6, 0.1, 9, 10, 11] My BUY signle is flashin at price=3, indicator=0.6 and index=1 现在我想从收盘价[index:] 开始,直到我找到第一个价格,例如可以说小于 10 * 0.6(value来自索引 2 的指标)。如果它是真的,我会在我的卖出数组中放一个 True。在我们的例子中,5 将满足条件。最后我会有。 buy = [False, True, False, False, False]sell = [False, False, False, True, False]
  • 所以告诉我这是否正确:对于buy == True 所在的每一行,我们希望将该行的indicator 以及该行之后的所有close 值传递给conditions()功能?
  • 我们只想传递来自indicator 的一个值,该值与buy==True 位于同一索引处,以及该条件索引之后的所有关闭值。并且条件应该检查​​ indicator 乘以 10 的特定值是否大于我们正在检查的当前值 close
  • 很高兴您能理解,谢谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 2019-05-24
  • 1970-01-01
  • 2014-03-27
  • 2020-12-14
  • 2021-10-01
相关资源
最近更新 更多