使用loc或iloc编写过滤数据的多个条件的有效方法

时间:2018-03-08 11:30:36

标签: python pandas loc

我编写了如下代码来过滤掉名为'文档类型'的列中的记录。其中包含大约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')] 

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

提前致谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

我将使用toIgnore = ['AVC', 'CC', 'CDI', 'BSX', 'BTR', 'FAF'] salesdf[~salesdf['Document type'].isin(toIgnore)] 和否定:

{{1}}