Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping rows with contain of a list of certain strings in Pandas [duplicate]

Tags:

python

pandas

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'
like image 633
thor Avatar asked Oct 24 '25 01:10

thor


1 Answers

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
like image 126
timgeb Avatar answered Oct 26 '25 16:10

timgeb