Bottom Page

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Sample based on the distribution of a feature to create more balanced data set dervast 0 69 Sep-25-2019, 05:56 PM
Last Post: dervast
  select data based on indice Staph 4 234 Jul-15-2019, 02:05 AM
Last Post: scidam
  Pandas fillna based on conditions amyd 1 1,897 May-03-2019, 11:27 AM
Last Post: scidam
  Pandas .rolling() with some calculations inside irmscher 5 665 Apr-04-2019, 11:55 AM
Last Post: scidam
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 518 Mar-26-2019, 11:49 AM
Last Post: scidam
  Grouping Integers mjack24 4 391 Feb-08-2019, 11:12 AM
Last Post: FranSPG
  Numpy Rolling mean window Thunberd 1 1,438 Jun-14-2018, 01:37 AM
Last Post: Larz60+
  Creating a matrix of rolling variances vvvcvvcv 1 645 May-26-2018, 12:51 PM
Last Post: killerrex
  Updating df rows based on 2 conditions stretch 1 755 May-02-2018, 09:15 AM
Last Post: volcano63
  Rolling sum for a window of 2 days (Pandas) klllmmm 1 5,118 Feb-02-2018, 03:24 PM
Last Post: klllmmm

Forum Jump:


Users browsing this thread: 1 Guest(s)