python - 删除列表中包含值的行

标签 python filter pandas python-2.x delete-row

我正在尝试过滤掉大型数据框,并且不希望“产品描述”列中包含包含某些值的行。

我看过了 how can i remove multiple rows with different labels in one command in pandas?

Remove rows not .isin('X')

并应用了代码。然而,

  df[-df['label'].isin(List)] 

不适合我,我不知道该怎么做。

这是我的确切代码:

List2 = ['set up','setup','and install',....etc etc]

(我也尝试过使用括号而不是方括号的 List2 = ( ..etc ),但它不起作用)

Computers_No_UNSPSC =Compters_No_UNSPSC[- Computers_No_UNSPSC['Product Description'].isin(List2)]

(我也尝试使用 ~ 而不是 - ,但这不起作用)

我是否做错/遗漏了什么?当我查看 Computers_No_UNSPSC 数据框时,我发现有些行仍然包含我创建的列表中的单词。它似乎没有过滤掉我不想要的东西。

感谢您的帮助!

**我相信 List2 正在工作。我有几行数据,人们描述他们购买的电脑。我希望购买的所有计算机都不是“计算机维修”或“计算机软件”。 所以我创建了一个列表,似乎捕获了我不想要的外围设备/东西......好吧,当我说

print List2 

我明白了

['set up', 'setup', 'and install', ' server', 'labor', 'services', 'processing', 'license', 'renewal', 'repair', 'case', 'speakers', 'cord', 'support', 'cart', 'docking station', 'components', 'accessories', 'software', ' membership', ' headsets ', ' keyboard', ' mouse', ' peripheral', ' part', ' charger', ' battery', ' drive', ' print', ' cable', ' supp', ' usb', ' shelf', 'disk', 'memory', 'studio', 'training', 'adapter', 'wiring', 'mirror']

这是否意味着它将每个字符串识别为一个单词?所以当我应用过滤器时,它会过滤 List2 中的每个单词?

A =A[-A['Product Description'].isin(List2)] 

这似乎是不起作用的部分,但我再次不确定我哪里出错了。

最佳答案

我认为您不明白它是如何工作的,它检查 if label == 该列表中的任何内容...而不是 if label 包含该列表中的任何内容...

听起来标签可能看起来像

label = "set up computer"

isin 将查找完全匹配...而不是部分匹配

label in ["set","up","computer"] #is false for example
"set" in ["set","up","computer"] #is true for example

注意:这显然不是 pandas isin 但工作原理相同......

要做你想做的事,你需要对照标签检查单词列表

any(word in label for word in blacklisted_words)

这会慢很多

关于python - 删除列表中包含值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864814/

相关文章:

python - 有条件地填充 Pandas 数据框中的空白值

python-3.x - 我如何根据纪元时间 ('attempt_updated_at' 列获得前半部分和后半部分)

python - 用于 boto3 的 PyCharm 智能感知

Python/Django : Associative arrays error

Javascript reduce - 一个参数采用错误的值

filter - 卡尔曼滤波器与指数滤波器

python - pandas groupby 作为列表返回空,同时使用 agg 和 apply 函数

Python:递归函数中的错误处理

python - 如何使用 PyPlot 摆脱鼠标悬停坐标?

filter - 在 Power BI 中筛选时显示相关数据