[英]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).我有一个带有一些列的 dataframe,其中之一是包含一些
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.此列的几个单元格中有“无文本”,但我注意到(我不知道为什么)有一些空格:例如在某些行中我有
"no text"
在其他" no text"
中, " no text "
和" no text "
等。
I thought to use a condition like this to remove the rows whose column Text
misses it:我想使用这样的条件来删除其列
Text
错过它的行:
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?但给了我一些错误(
KeyError: '[False] not found in axis'
)我知道对于这样的东西,必须通过 boolean 条件, df = df.drop(df[boolean_cond])
那我在做什么错误的?
If you want to remove rows which contain string as no text
then you can do this:如果要删除包含字符串为
no text
的行,则可以执行以下操作:
data = data[~(data['Text'].str.contains("no text"))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.