我正在尝试过滤出较大的数据框,并且不希望在“产品说明”列中包含某些值的行。
我看过
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)]
这似乎是无法正常工作的部分,但同样,我不确定哪里出了问题。
最佳答案
我不认为您了解如果label ==该列表中的任何内容,而不是如果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/