Skip to content
Advertisement

Get only data that are repeated any one of the given year in pandas

Below is the Raw Data. I want only those event which will repeat in given set of list of years. Eg [2012,2013]. So now it should only get data if event is repeated in one of the given year in the list.

    Event   Year
Event 1     2012
Event 1     2013
Event 1     2014
Event 1     2015
Event 2     2012
Event 2     2013
Event 3     2014
Event 3     2015
Event 3     2014
Event 4     2012
Event 4     2015
Event 5     2013

I want below output.

    Event   Year
Event 1     2012
Event 1     2013
Event 1     2014
Event 1     2015
Event 2     2012
Event 2     2013
Event 4     2012
Event 4     2015
Event 5     2013

Advertisement

Answer

You can try groupby and filter

lst = [2012, 2013]
out = df.groupby('Event').filter(lambda g: g['Year'].isin(lst).any())
print(out)

      Event  Year
0   Event 1  2012
1   Event 1  2013
2   Event 1  2014
3   Event 1  2015
4   Event 2  2012
5   Event 2  2013
9   Event 4  2012
10  Event 4  2015
11  Event 5  2013
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement