user12941222
user12941222

Reputation:

remove row in pandas column based on "if string in cell" condition

I have a dataframe with some columns, one of this is Text that contains some text (obv).

Several cells of this columns have "no text" in there, but I have noticed ( I don't know why) that there are some spaces: for example in some rows I have "no text" in others " no text" , " no text " and " no text " and so on.

I thought to use a condition like this to remove the rows whose column Text misses it:

data = data.drop(data['no text' in data['Text']].index)

but gives me some errors (KeyError: '[False] not found in axis') I know that for stuff like this, one have to pass a boolean condition, df = df.drop(df[boolean_cond]) so what am I doing wrong?

Upvotes: 0

Views: 2120

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

Series.str.contains

If you want to remove rows which contain string as no text then you can do this:

data = data[~(data['Text'].str.contains("no text"))]

Upvotes: 3

Related Questions