vicky
vicky

Reputation: 49

Pandas groupby: checking gaps within a group

This is my data frame, If there is a gap in Year for a particular name, Gap column should be True else False.

Name    Year       Gap
A       2008      False
A       2008      False
A       2009      True
A       2011      False
B       2010      True 
B       2013      False

Upvotes: 0

Views: 481

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can do:

df['Gap'] = df.groupby('Name')['Year'].diff(-1).lt(-1)

Output:

0    False
1    False
2     True
3    False
4     True
5    False
Name: Year, dtype: bool

Upvotes: 1

Related Questions