新熊猫列具有取决于上一行条件的累积值

时间:2019-07-03 18:10:46

标签: python pandas numpy

我有以下由三列组成的数据框:值,求和,ema。

df1['equity_cond'] = np.where(df1['cumsum'].shift(1)>=df1['ema'].shift(1), df1['equity_cond'].shift(1) + df1['values'], df1['equity_cond'].shift(1))

Image 1

我想创建一个新列,如果在上一行中“ cumsum”的值等于或大于“ ema”,则添加累积的“ values”列。

我已经尝试过了:

KeyError: 'equity_cond'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-25-1c89c1e1113c> in <module>
      ----> 2 df1['equity_cond'] = np.where(df1['cumsum'].shift(1)>=df1['ema'].shift(1), df1['equity_cond'].shift(1) + df1['values'], df1['equity_cond'].shift(1))

哪个会产生以下错误:

0
0
0
0
0
31.6
83.6
…

这如何在熊猫中实现?


编辑:

此新列的预期值为:

std::set_intersection

2 个答案:

答案 0 :(得分:1)

您可以将['values']列乘以条件,以便在应用.cumsum()时,它为不满足条件的行加0,并为{{1} }列。

['values']

这似乎可以为您提供所需的结果:

cond = df1['cumsum'].shift() >= df1['ema'].shift()
df1['x'] = np.where(cond,
                   (df1['values'] * cond).cumsum(),
                   0)

答案 1 :(得分:0)

两种方法:

1。。使用的是熊猫,类似于布伦丹的建议。

cond = df1['cumsum'].shift() >= df1['ema'].shift()
df1['equity_cond'] = (df1['values'] * cond).cumsum()


2。或这样做,然后将列添加到df1:

# your previous code goes here
equity = [0 for x in range(len(df1))]

for i, n in enumerate(equity):
    if i==0: continue
    if df1["cumsum"][i-1] >= df1["ema"][i-1]:
        equity[i] = equity[i-1] + df1["values"][i]
    else:
        equity[i] = equity[i-1]

df1["equity_cond"] = equity