##### Grouping data based on rolling conditions
 Grouping data based on rolling conditions kapilan15 Programmer named Tim Posts: 11 Threads: 9 Joined: Dec 2018 Reputation: 0 Jun-05-2019, 01:07 PM I am trying to group the data-frame based on a few conditions. Data frame: Start Date End Date value 1971-07-01 1971-07-31 0.0 1971-08-01 1971-08-31 0.25 1971-09-01 1971-09-30 -0.62 1971-10-01 1971-10-31 0.0 1971-11-01 1971-11-30 -0.63 1971-12-01 1971-12-31 -1.0 1972-01-01 1972-01-31 0.0 1972-02-01 1972-02-29 0.0 1972-03-01 1972-03-31 2.0 1972-04-01 1972-04-30 0.0 . . 1973-07-01 1973-07-31 2.0 1973-08-01 1973-08-31 0.5 1973-09-01 1973-09-30 -2.0 1973-10-01 1973-10-31 0.0 1973-11-01 1973-11-30 0.0 1973-12-01 1973-12-31 0.0 1974-01-01 1974-01-31 0.0 1974-02-01 1974-02-28 0.0 . . . 1974-11-01 1974-11-30 0.0 1974-12-01 1974-12-31 -1.25 1975-01-01 1975-01-31 -1.0 1975-02-01 1975-02-28 -1.0 1975-03-01 1975-03-31 -0.5 1975-04-01 1975-04-30 -0.25 1975-05-01 1975-05-31 0.0 1975-06-01 1975-06-30 1.25 1975-07-01 1975-07-31 0.0 1975-08-01 1975-08-31 0.0 Criteria for grouping The group should always start with the negative value The group continues as long as we have a negative value The group ends if we reach either a positive value or three consecutive zeros. An example that fulfills the criteria from the above data frame 1971-09-01 1971-09-30 -0.62 1971-10-01 1971-10-31 0.0 1971-11-01 1971-11-30 -0.63 1971-12-01 1971-12-31 -1.0 1972-01-01 1972-01-31 0.0 1972-02-01 1972-02-29 0.0 I tried for loop but I am not going anywhere with that. ```for i in df.index: no = 0 if df['Value'][i] < 0: df['groupno'] = no```Thanks for reading. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Hurst Exponent in Rolling Basis illmattic 1 171 Jan-06-2021, 09:49 PM Last Post: illmattic Calculating Beta over Rolling Periods illmattic 2 368 Sep-27-2020, 11:27 PM Last Post: Larz60+ Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 382 Aug-03-2020, 02:05 AM Last Post: scidam sns point plot grouping Mekala 0 285 Jul-24-2020, 04:06 PM Last Post: Mekala Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 659 Jul-15-2020, 06:20 PM Last Post: pawanmtm Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 553 Jun-09-2020, 06:05 AM Last Post: Pan Counting and grouping Kudzo 0 379 Jan-27-2020, 01:30 AM Last Post: Kudzo Apply rolling window function over time dimension of 3D data Staph 0 642 Jan-01-2020, 08:31 AM Last Post: Staph Sample based on the distribution of a feature to create more balanced data set dervast 1 543 Nov-15-2019, 12:25 AM Last Post: schuler select data based on indice Staph 4 754 Jul-15-2019, 02:05 AM Last Post: scidam

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020