[英]Pandas data frame adding a column based on data from 2 other columns
I have a data frame.我有一个数据框。 One column is called fractal.
一列称为分形。 It has 0's or 1's in which the 1's represents a fractal.
它有 0 或 1,其中 1 代表分形。 Here is the output of
np.flatnonzero
to get an idea of the frequency of fractals:这是 np.flatnonzero 的
np.flatnonzero
以了解分形的频率:
np.flatnonzero
[ 15 32 77 93 110 152 165 185 194 201 223 232 245 264 294 306 320 327
347 370 380 391 409 436 447 460 474 481 500 534 549 561 579 586 599 620
627 641 653 670 685 704 711 758 784]
There's another column that has a high price, df['high']
that contains the daily high prices of a financial instrument.还有另一列具有高价,
df['high']
包含金融工具的每日最高价。
I want to add a column to the database, df['f_support']
that contains high prices relating to the high price of the last fractal.我想在数据库中添加一列
df['f_support']
,其中包含与最后一个分形的高价相关的高价。
The high price is 2 rows before the fractal signal.最高价位于分形信号之前的 2 行。 In other words, the column would contain the same high price until another fractal signal, then a new high price would start filling the column.
换句话说,该柱将包含相同的最高价,直到出现另一个分形信号,然后新的高价将开始填充该柱。
Looking at the output of np.flatzero
the column f_support
should contain this:查看 np.flatzero 的
np.flatzero
列f_support
应包含以下内容:
f_support ![]() |
value![]() |
---|---|
0–14 ![]() |
nothing![]() |
15–31 ![]() |
df['high'].iloc[13] |
32–77 ![]() |
df['high'].iloc[30] |
and so on.等等。
I hope I've conveyed this so it makes sense.我希望我已经传达了这一点,所以它是有道理的。 There's probably an easy way to do this but it's beyond my present scope.
可能有一种简单的方法可以做到这一点,但它超出了我目前的 scope。
IIUC: IIUC:
fracloc = np.flatnonzero(df.fractal)
df.loc[df.index[fracloc], 'f_support'] = df['high'].iloc[fracloc - 2].to_numpy()
df['f_support'] = df['f_support'].pad()
df
fractal high f_support
0 0 74.961120 NaN
1 0 2.297611 NaN
2 0 60.294702 NaN
3 0 91.874424 NaN
4 0 69.327601 NaN
.. ... ... ...
73 0 34.925407 61.977998
74 0 64.475880 61.977998
75 0 86.939800 61.977998
76 0 42.377974 61.977998
77 1 42.725907 86.939800
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.