4

I'm trying to drop rows which contain certain sub strings in a column. I want to drop all values that contain the sub strings 'Year', 'Monday', '/'

My dataframe looks like:

col1
24/05/2020
May Year 2020
Monday
May 2020

The code I tried:

drop_values = ['Monday','Year', '/']
df = df[~df['Col1'].str.contains(drop_values)]

However I get the following error:

TypeError: unhashable type: 'list'
AMC
  • 2,642
  • 7
  • 13
  • 35
thor
  • 281
  • 1
  • 6
  • 13

2 Answers2

8

The Series.str.contains method accepts a regex.

>>> df
            col1
0     24/05/2020
1  May Year 2020
2         Monday
3       May 2020
>>> drop_values = ['Monday','Year', '/']
>>> df[~df['col1'].str.contains('|'.join(drop_values))]
       col1
3  May 2020
timgeb
  • 76,762
  • 20
  • 123
  • 145
3

You can do this:

df = df[~df['col1'].str.contains('Monday|Year|/')]
print(df)

Output:

       col1
3  May 2020
NYC Coder
  • 7,424
  • 2
  • 11
  • 24