I want to filter rows from data frame where the year is 2020 and 2021 using re.search and re.match functions



Data Frame:

    Unnamed: 0  date    target  insult  tweet   year
0   1   2014-10-09  thomas-frieden  fool    Can you believe this fool, Dr. Thomas Frieden ...   2014
1   2   2014-10-09  thomas-frieden  DOPE    Can you believe this fool, Dr. Thomas Frieden ...   2014
2   3   2015-06-16  politicians all talk and no action  Big time in U.S. today - MAKE AMERICA GREAT AG...   2015
3   4   2015-06-24  ben-cardin  It's politicians like Cardin that have destroy...   Politician @SenatorCardin didn't like that I s...   2015
4   5   2015-06-24  neil-young  total hypocrite For the nonbeliever, here is a photo of @Neily...   2015

I want the data frame which consists for only year with 2020 and 2021 using search and match methods.

Answer

df_filtered = df.loc[df.year.str.contains('2014|2015', regex=True) == True]


Source: stackoverflow