1

我已经编写了如下代码来过滤掉名为“文档类型”的列中的记录,该列包含大约 25 个分类值。

salesdf.loc[(salesdf['Document type'] != 'AVC') & 
(salesdf['Document type'] != 'CC') & 
(salesdf['Document type'] != 'CDI') & 
(salesdf['Document type'] != 'BSX') & 
(salesdf['Document type'] != 'BTR') & 
(salesdf['Document type'] != 'FAF')] 

我只是想知道是否有一种有效的编写代码的方法可以给我相同的输出?

4

2 回答 2

3

我认为您需要isin通过以下方式反转条件~

salesdf[~salesdf['Document type'].isin(['AVC', 'CC','CDI', 'BSX','BTR','FAF'])]

样品

salesdf = pd.DataFrame({
    'Document type': ['AVC','CDI','CC','a','b','FAF','BTR','c','BSX']
})
print (salesdf)
  Document type
0           AVC
1           CDI
2            CC
3             a
4             b
5           FAF
6           BTR
7             c
8           BSX

a = salesdf.loc[(salesdf['Document type'] != 'AVC') & 
(salesdf['Document type'] != 'CC') & 
(salesdf['Document type'] != 'CDI') & 
(salesdf['Document type'] != 'BSX') & 
(salesdf['Document type'] != 'BTR') & 
(salesdf['Document type'] != 'FAF')] 

print (a)
  Document type
3             a
4             b
7             c

b = salesdf[~salesdf['Document type'].isin(['AVC', 'CC','CDI', 'BSX','BTR','FAF'])]
print (b)
  Document type
3             a
4             b
7             c
于 2018-03-08T11:33:04.920 回答
1

我将使用.isin()和否定:

toIgnore = ['AVC', 'CC', 'CDI', 'BSX', 'BTR', 'FAF']
salesdf[~salesdf['Document type'].isin(toIgnore)]
于 2018-03-08T11:35:29.960 回答