簡體   English   中英

根據“if string in cell”條件刪除 pandas 列中的行

[英]remove row in pandas column based on “if string in cell” condition

我有一個帶有一些列的 dataframe,其中之一是包含一些Text (obv)的文本。

此列的幾個單元格中有“無文本”,但我注意到(我不知道為什么)有一些空格:例如在某些行中我有"no text"在其他" no text"中, " no text "" no text "等。

我想使用這樣的條件來刪除其列Text錯過它的行:

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

但給了我一些錯誤( KeyError: '[False] not found in axis' )我知道對於這樣的東西,必須通過 boolean 條件, df = df.drop(df[boolean_cond])那我在做什么錯誤的?

Series.str.contains

如果要刪除包含字符串為no text的行,則可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM