简体   繁体   中英

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. 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 ”

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)

the crossover part is just another condition that is -1 or 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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM