How to filter pandas framework for cells that DO NOT contain substring?

I want to filter a dataframe to find rows that do not contain the string 'site'.

I know how to filter strings that contain a "site", but could not get the reverse work. Here is what I still have:

def rbs(): #removes blocked sites
    frame = fill_rate()
    mask = frame[frame['Media'].str.contains('Site')==True]
    frame = (frame != mask)
    return frame

But this, of course, returns an error.

+4
source share
1 answer

Just do frame[~frame['Media'].str.contains('Site')]

~ denies a logical condition

So your method will look like this:

def rbs(): #removes blocked sites
    frame = fill_rate()
    return frame[~frame['Media'].str.contains('Site')]

EDIT

it looks like you have values NaNjudging by your mistakes, so you need to filter them first so your method becomes:

def rbs(): #removes blocked sites
    frame = fill_rate()
    frame = frame[frame['Media'].notnull()]
    return frame[~frame['Media'].str.contains('Site')]

notnull will filter out missing values

+9
source

All Articles