Python Forum
Grouping data based on rolling conditions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grouping data based on rolling conditions
#1
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


Messages In This Thread
Grouping data based on rolling conditions - by kapilan15 - Jun-05-2019, 01:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 662 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  Supervised learning, tree based model - problems splitting data Pixel 0 667 May-16-2023, 05:25 PM
Last Post: Pixel
  Grouping Data based on 30% bracket purnima1 0 954 Feb-16-2023, 07:14 PM
Last Post: purnima1
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 887 Dec-12-2022, 03:22 AM
Last Post: ill8
  Rolling window and apply code JunkBoy 6 1,865 Jul-23-2022, 07:00 PM
Last Post: JunkBoy
  New Dataframe Column Based on Several Conditions nb1214 1 1,800 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,691 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Hurst Exponent in Rolling Basis illmattic 1 3,848 Jan-06-2021, 09:49 PM
Last Post: illmattic
  Calculating Beta over Rolling Periods illmattic 2 5,373 Sep-27-2020, 11:27 PM
Last Post: Larz60+
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,215 Aug-03-2020, 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