简体   繁体   English

如何从包含特定列中的特定字符串(多个)的 Pandas 数据框中删除行?

[英]How to drop rows from pandas data frame that contains a particular string(multiple) in a particular column?

I have been trying this creating multiple dataframes to create multiple strings, but I am not able to remove strings more than 2 only thing is i wanted multiple strings to be removed.我一直在尝试创建多个数据框来创建多个字符串,但是我无法删除超过 2 个的字符串,唯一的问题是我想要删除多个字符串。

data3 = data[~data.column.str.contains("remove words")]
data3 = data3[~data3.column.str.contains("remove me")]

data3.count

I have tried this but no good.我试过这个,但不好。

df = df[~df.column.isin(['remove words'])]

or或者

df = df[~df.column.isin(['remove words', 'remove me'])]

You simply need to add loc .您只需要添加loc When a boolean mask is applied to a dataframe/series, only the explicit loc notation will do the trick.当布尔掩码应用于数据帧/系列时,只有显式的 loc 符号才能起作用。

df.loc[~df.column.isin(['remove words', 'remove me'])]

I think you were on the right path.我认为你走在正确的道路上。

Let's define a toy dataframe:让我们定义一个玩具数据框:

>>> df = pd.DataFrame([("i have a car", 2), 
    ("cows make milk", 3), 
    ("try this remove me stuff", 5), 
    ("please remove words", 51)], 
    columns=["text", "number"])

And here you go:你去吧:

>>> words_to_avoid = ["remove me", "remove words"]
>>> df[df.text.apply(
        lambda txt: not any([word_to_avoid in txt for word_to_avoid in words_to_avoid])
    )]

    text    number
0   car       2
1   cow       3

试试这个方法:

df2 = df1[~df1.column.str.contains('remove words|remove me', regex=True)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从包含特定列中的任何字符串的Pandas数据框中删除行 - How to Remove Rows from Pandas Data Frame that Contains any String in a Particular Column 在python中,如何从任何列中存在特定字符串的数据框中获取行(字符串值) - In python,how to get the rows from a data frame where a particular string is present in any of the column (String value) 熊猫数据框找到具有特定列值的所有行? - pandas data frame find all rows with particular column value? 如果列的字符串值包含特定模式,如何从 pandas 数据帧中提取整行 - How to extract entire rows from pandas data frame, if a column's string value contains a specific pattern 如何丢弃重复项但如果某个特定的其他列不为空则保留行(Pandas) - How to drop duplicates but keep the rows if a particular other column is not null (Pandas) 熊猫数据框,不包括特定范围内的行 - Pandas data frame excluding rows in a particular range 如果列匹配特定字符串,则在数据框中删除行 - Drop rows in dataframe if the column matches particular string 如何根据特定列中的值对数据帧的行求平均? - How to average rows of a data frame based the value in a particular column? 在 Pandas 数据框中为其查找特定的字符串和值 - Find particular string and value for it in Pandas data frame 从 Pandas 数据框中选择多行,其中一列包含一些作为 NaN 的值 - Select multiple rows from pandas data frame where one of column contains some values as NaN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM