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.
JavaScript
x
14
14
1
Event Year
2
Event 1 2012
3
Event 1 2013
4
Event 1 2014
5
Event 1 2015
6
Event 2 2012
7
Event 2 2013
8
Event 3 2014
9
Event 3 2015
10
Event 3 2014
11
Event 4 2012
12
Event 4 2015
13
Event 5 2013
14
I want below output.
JavaScript
1
11
11
1
Event Year
2
Event 1 2012
3
Event 1 2013
4
Event 1 2014
5
Event 1 2015
6
Event 2 2012
7
Event 2 2013
8
Event 4 2012
9
Event 4 2015
10
Event 5 2013
11
Advertisement
Answer
You can try groupby
and filter
JavaScript
1
3
1
lst = [2012, 2013]
2
out = df.groupby('Event').filter(lambda g: g['Year'].isin(lst).any())
3
JavaScript
1
13
13
1
print(out)
2
3
Event Year
4
0 Event 1 2012
5
1 Event 1 2013
6
2 Event 1 2014
7
3 Event 1 2015
8
4 Event 2 2012
9
5 Event 2 2013
10
9 Event 4 2012
11
10 Event 4 2015
12
11 Event 5 2013
13