简体   繁体   中英

Pandas count True/False from last row since a switch

I have a df like this:

             timestamp   open   high    low  close    volume  previous_close  high-low  high-pc  low-pc    tr       atr  upperband  lowerband  in_uptrend
89 2022-08-01 03:50:00  65.07  65.29  65.07  65.21  1846.148           65.08      0.22     0.21    0.01  0.22  0.190000  65.341429  64.610000       False
90 2022-08-01 03:55:00  65.21  65.29  65.08  65.16  1498.980           65.21      0.21     0.08    0.13  0.21  0.185714  65.341429  64.627857       False
91 2022-08-01 04:00:00  65.16  65.27  65.14  65.26  1403.845           65.16      0.13     0.11    0.02  0.13  0.185714  65.341429  64.647857       False
92 2022-08-01 04:05:00  65.24  65.28  64.93  64.97  1301.370           65.26      0.35     0.02    0.33  0.35  0.220000  65.341429  64.445000       False
93 2022-08-01 04:10:00  64.96  65.09  64.96  65.05   975.097           64.97      0.13     0.12    0.01  0.13  0.220000  65.341429  64.365000       False
94 2022-08-01 04:15:00  65.04  65.05  64.90  64.98   820.264           65.05      0.15     0.00    0.15  0.15  0.224286  65.341429  64.302143       False
95 2022-08-01 04:20:00  65.00  65.32  64.95  65.29   797.967           64.98      0.37     0.34    0.03  0.37  0.222857  65.341429  64.466429       False
96 2022-08-01 04:25:00  65.30  65.33  65.19  65.25   720.771           65.29      0.14     0.04    0.10  0.14  0.211429  65.341429  64.625714       False
97 2022-08-01 04:30:00  65.26  65.44  65.24  65.41  1067.216           65.25      0.20     0.19    0.01  0.20  0.210000  65.970000  64.710000        True
98 2022-08-01 04:35:00  65.40  65.47  65.22  65.22   951.461           65.41      0.25     0.06    0.19  0.25  0.227143  66.026429  64.710000        True

if the last row of 'in_uptrend' is True I want to write in an other row the quantity since the switch from False to True, in my df it's 2 ( there is 2 True since the last False)

Same if the last row is False, I would like to the quantity of False after the switch from True to False.

Expected Result could be like this:

             timestamp   open   high    low  close    volume  previous_close  high-low  high-pc  low-pc    tr       atr  upperband  lowerband  in_uptrend count_uptrend
89 2022-08-01 03:50:00  65.07  65.29  65.07  65.21  1846.148           65.08      0.22     0.21    0.01  0.22  0.190000  65.341429  64.610000       False
90 2022-08-01 03:55:00  65.21  65.29  65.08  65.16  1498.980           65.21      0.21     0.08    0.13  0.21  0.185714  65.341429  64.627857       False
91 2022-08-01 04:00:00  65.16  65.27  65.14  65.26  1403.845           65.16      0.13     0.11    0.02  0.13  0.185714  65.341429  64.647857       False
92 2022-08-01 04:05:00  65.24  65.28  64.93  64.97  1301.370           65.26      0.35     0.02    0.33  0.35  0.220000  65.341429  64.445000       False
93 2022-08-01 04:10:00  64.96  65.09  64.96  65.05   975.097           64.97      0.13     0.12    0.01  0.13  0.220000  65.341429  64.365000       False
94 2022-08-01 04:15:00  65.04  65.05  64.90  64.98   820.264           65.05      0.15     0.00    0.15  0.15  0.224286  65.341429  64.302143       False
95 2022-08-01 04:20:00  65.00  65.32  64.95  65.29   797.967           64.98      0.37     0.34    0.03  0.37  0.222857  65.341429  64.466429       False
96 2022-08-01 04:25:00  65.30  65.33  65.19  65.25   720.771           65.29      0.14     0.04    0.10  0.14  0.211429  65.341429  64.625714       False
97 2022-08-01 04:30:00  65.26  65.44  65.24  65.41  1067.216           65.25      0.20     0.19    0.01  0.20  0.210000  65.970000  64.710000        True
98 2022-08-01 04:35:00  65.40  65.47  65.22  65.22   951.461           65.41      0.25     0.06    0.19  0.25  0.227143  66.026429  64.710000        True        2

or like this would be better:

             timestamp   open   high    low  close    volume  previous_close  high-low  high-pc  low-pc    tr       atr  upperband  lowerband  in_uptrend count_uptrend
89 2022-08-01 03:50:00  65.07  65.29  65.07  65.21  1846.148           65.08      0.22     0.21    0.01  0.22  0.190000  65.341429  64.610000       False        1
90 2022-08-01 03:55:00  65.21  65.29  65.08  65.16  1498.980           65.21      0.21     0.08    0.13  0.21  0.185714  65.341429  64.627857       False        2
91 2022-08-01 04:00:00  65.16  65.27  65.14  65.26  1403.845           65.16      0.13     0.11    0.02  0.13  0.185714  65.341429  64.647857       False        3
92 2022-08-01 04:05:00  65.24  65.28  64.93  64.97  1301.370           65.26      0.35     0.02    0.33  0.35  0.220000  65.341429  64.445000       False        4
93 2022-08-01 04:10:00  64.96  65.09  64.96  65.05   975.097           64.97      0.13     0.12    0.01  0.13  0.220000  65.341429  64.365000       False        5
94 2022-08-01 04:15:00  65.04  65.05  64.90  64.98   820.264           65.05      0.15     0.00    0.15  0.15  0.224286  65.341429  64.302143       False        6
95 2022-08-01 04:20:00  65.00  65.32  64.95  65.29   797.967           64.98      0.37     0.34    0.03  0.37  0.222857  65.341429  64.466429       False        7
96 2022-08-01 04:25:00  65.30  65.33  65.19  65.25   720.771           65.29      0.14     0.04    0.10  0.14  0.211429  65.341429  64.625714       False        8
97 2022-08-01 04:30:00  65.26  65.44  65.24  65.41  1067.216           65.25      0.20     0.19    0.01  0.20  0.210000  65.970000  64.710000        True        1
98 2022-08-01 04:35:00  65.40  65.47  65.22  65.22   951.461           65.41      0.25     0.06    0.19  0.25  0.227143  66.026429  64.710000        True        2

IIUC, use cumcount :

df['count_uptrend'] = (df.groupby(df['in_uptrend']
                                  .ne(df['in_uptrend'].shift())
                                  .cumsum()
                                 )
                         .cumcount().add(1)
                       )

Output:

             timestamp   open   high    low  close    volume  previous_close  high-low  high-pc  low-pc    tr       atr  upperband  lowerband  in_uptrend count_uptrend
89 2022-08-01 03:50:00  65.07  65.29  65.07  65.21  1846.148           65.08      0.22     0.21    0.01  0.22  0.190000  65.341429  64.610000       False        1
90 2022-08-01 03:55:00  65.21  65.29  65.08  65.16  1498.980           65.21      0.21     0.08    0.13  0.21  0.185714  65.341429  64.627857       False        2
91 2022-08-01 04:00:00  65.16  65.27  65.14  65.26  1403.845           65.16      0.13     0.11    0.02  0.13  0.185714  65.341429  64.647857       False        3
92 2022-08-01 04:05:00  65.24  65.28  64.93  64.97  1301.370           65.26      0.35     0.02    0.33  0.35  0.220000  65.341429  64.445000       False        4
93 2022-08-01 04:10:00  64.96  65.09  64.96  65.05   975.097           64.97      0.13     0.12    0.01  0.13  0.220000  65.341429  64.365000       False        5
94 2022-08-01 04:15:00  65.04  65.05  64.90  64.98   820.264           65.05      0.15     0.00    0.15  0.15  0.224286  65.341429  64.302143       False        6
95 2022-08-01 04:20:00  65.00  65.32  64.95  65.29   797.967           64.98      0.37     0.34    0.03  0.37  0.222857  65.341429  64.466429       False        7
96 2022-08-01 04:25:00  65.30  65.33  65.19  65.25   720.771           65.29      0.14     0.04    0.10  0.14  0.211429  65.341429  64.625714       False        8
97 2022-08-01 04:30:00  65.26  65.44  65.24  65.41  1067.216           65.25      0.20     0.19    0.01  0.20  0.210000  65.970000  64.710000        True        1
98 2022-08-01 04:35:00  65.40  65.47  65.22  65.22   951.461           65.41      0.25     0.06    0.19  0.25  0.227143  66.026429  64.710000        True        2

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