简体   繁体   中英

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?

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"))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM