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

Time:06-10

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

CodePudding user response:

I would think two isin operations:

valid_events = df.loc[df.Year.isin([2012,2013]), 'Event']
df.loc[df.Event.isin(valid_events)]

Output:

      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

CodePudding user response:

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
  • Related