1

Have a Pandas Dataframe like below.

EventOccurrence Month
1                4
1                5
1                6
1                9
1                10
1                12

Need to add a identifier column to above panda's dataframe such that whenever Month is consecutive thrice a value of True is filled, else false. Explored few options like shift and window without luck. Any pointer is appreciated.

EventOccurrence Month Flag
1               4       F  
1               5       F
1               6       T
1               9       F
1               10      F
1               12      F

Thank You.

1 Answer 1

1

You can check whether the diff between rows is one, and the diff shifted by 1 is one as well:

df['Flag'] = (df.Month.diff() == 1) & (df.Month.diff().shift() == 1)

   EventOccurrence  Month   Flag
0                1      4  False
1                1      5  False
2                1      6   True
3                1      9  False
4                1     10  False
5                1     12  False

Note that this will also return True if it is consecutive > 3 times, but that behaviour wasn't specified in the question so I'll assume it's OK

If it needs to only flag the third one, and not for example the fourth consecutive instance, you could add a condition:

df['Flag'] = (df.Month.diff() == 1) & (df.Month.diff().shift() == 1) & (df.Month.diff().shift(2) !=1)
Sign up to request clarification or add additional context in comments.

1 Comment

superb...Thanks for your time..a crisp and correct answer. Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.