[英]Iterating through Pandas rows after an initial condition in the same row is met
“I am trying to write a program that uses a pandas data.rsi and iterate through this column. “我正在尝试编写一个使用pandas data.rsi的程序并遍历此列。 if rsi > 70 i would like to check whether the n next data points have an rsi of 60, if the do and the rsi moves above 70 again a would like to create a 1 in a column called data.RSIFI ”
如果rsi> 70我想检查n个下一个数据点的rsi是否为60,如果do和rsi再次高于70,则希望在名为data.RSIFI的列中创建1。
To sum it up the problem is to look for a new condition in the n next rows when a condition is already met ( but not in the same state any longer) 总结一下,问题是当条件已满足时(但不再处于相同状态),在n个下一行中查找新条件
the crossover part is just another condition that is -1 or 1. 交叉部分只是-1或1的另一个条件。
for i in data.index:
val = data.get_value(i,'rsi')
if val >70 and data.get_value(i, 'cross_ov_un') == 1:
for n in range(40):
if val.shift(n) < 60:
for n in range(40):
if val.shift(n) > 70:
data.loc[i,'RSIFI']= 1
this does not work, and one of the problems is that a timestamp cant be shifted. 这不起作用,其中一个问题是时间戳无法移动。
example of how the data looks: 数据的外观示例:
RSIFI cross_ov_un rsi
date
2019-01-14 09:00:00 0 1 40.716622
2019-01-14 10:00:00 0 1 40.304055
2019-01-14 11:00:00 0 1 46.000142
2019-01-14 12:00:00 0 1 44.732117
2019-01-14 13:00:00 0 1 40.476486
2019-01-14 14:00:00 0 1 44.553255
2019-01-14 15:00:00 1 1 70.540997
2019-01-14 16:00:00 0 1 65.734665
2019-01-14 17:00:00 0 1 70.383329
2019-01-14 18:00:00 1 1 71.235720
2019-01-14 19:00:00 0 1 64.735780
2019-01-14 20:00:00 0 1 62.017401
2019-01-14 21:00:00 0 1 59.410495
2019-01-14 22:00:00 0 1 66.339052
2019-01-14 23:00:00 1 1 71.217073
2019-01-15 00:00:00 1 1 74.982245
2019-01-15 01:00:00 0 1 57.951364
2019-01-15 02:00:00 0 1 56.833347
Example of how I would like it to look 我希望它看起来如何的例子
RSIFI cross_ov_un rsi
date
2019-01-14 09:00:00 0 1 40.716622
2019-01-14 10:00:00 0 1 40.304055
2019-01-14 11:00:00 0 1 46.000142
2019-01-14 12:00:00 0 1 44.732117
2019-01-14 13:00:00 0 1 40.476486
2019-01-14 14:00:00 0 1 44.553255
2019-01-14 15:00:00 0 1 70.540997
2019-01-14 16:00:00 0 1 65.734665
2019-01-14 17:00:00 0 1 70.383329
2019-01-14 18:00:00 0 1 71.235720
2019-01-14 19:00:00 0 1 64.735780
2019-01-14 20:00:00 0 1 62.017401
2019-01-14 21:00:00 0 1 59.410495
2019-01-14 22:00:00 0 1 66.339052
2019-01-14 23:00:00 1 1 71.217073
2019-01-15 00:00:00 0 1 74.982245
2019-01-15 01:00:00 0 1 57.951364
2019-01-15 02:00:00 0 1 56.833347
The problem is that .loc
is used for accessing a group of rows, while the .at
method accesses the value of a single index of the data frame. 问题是
.loc
用于访问一组行,而.at
方法访问数据帧的单个索引的值。
for i in data.index:
val = data.at[i,'rsi']
if val > 70 and data.at[i, 'cross_ov_un'] == 1:
data.at[i,'RSIFI']= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.