【问题标题】:Find the first signal in a condition [duplicate]找到条件中的第一个信号[重复]
【发布时间】:2021-05-30 08:37:22
【问题描述】:

我有一个具有不同“价格”的数据框 (df),我想比较这些价格并做出决定。

df['Decision'] = np.where((df['price1'] > df['price2']) ,'sell',np.where((df['price1'] < df['price2']),'buy',np.nan))

我的输出是:

price1 price2 Decision
50 50 NaN
100 200 buy
70 140 buy
150 200 buy
150 50 sell
60 20 sell

但我只想拥有“买入”或“卖出”的“第一个信号”并删除复制直到下一个信号,如:

price1 price2 Decision
50 50 NaN
100 200 buy
70 140
150 200
150 50 sell
60 20

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

你可以试试idxmax:

df.loc[(df['price1'] > df['price2']).idxmax(), 'Decision'] = 'sell'
df.loc[(df['price1'] < df['price2']).idxmax(), 'Decision'] = 'buy'

【讨论】:

  • 它得到第一个表,而不是第二个。在决定改变之前,我不希望有相同的信号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2013-06-02
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多