Pandas conditional filtering on categorical values

I have the following table in pandas:

Index Ingredient Dish
1 Potato Pie
2 Potato Juice
3 Potato Fries
4 Potato Pure
5 Apple Pie
6 Apple Juice
7 Apple Fries
8 Apple Pure

and want to apply the following filter:
If Ingredient==’Apple’ then only keep Dish=[‘Pie’,’Juice’]

The result should look like this:

Index Ingredient Dish
1 Potato Pie
2 Potato Juice
3 Potato Fries
4 Potato Pure
5 Apple Pie
6 Apple Juice

How can I do that?

2

You want:

  • to keep any row that does not equal ‘Apple’ (‘Ingredient’), via Series.ne.
  • or (|) keep any row that is in ['Pie', 'Juice'] (‘Dish’), via Series.isin.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>m = df['Ingredient'].ne('Apple')
n = df['Dish'].isin(['Pie', 'Juice'])
df[m | n]
</code>
<code>m = df['Ingredient'].ne('Apple') n = df['Dish'].isin(['Pie', 'Juice']) df[m | n] </code>
m = df['Ingredient'].ne('Apple')
n = df['Dish'].isin(['Pie', 'Juice'])
df[m | n]

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Index Ingredient Dish
0 1 Potato Pie
1 2 Potato Juice
2 3 Potato Fries
3 4 Potato Pure
4 5 Apple Pie
5 6 Apple Juice
</code>
<code> Index Ingredient Dish 0 1 Potato Pie 1 2 Potato Juice 2 3 Potato Fries 3 4 Potato Pure 4 5 Apple Pie 5 6 Apple Juice </code>
   Index Ingredient   Dish
0      1     Potato    Pie
1      2     Potato  Juice
2      3     Potato  Fries
3      4     Potato   Pure
4      5      Apple    Pie
5      6      Apple  Juice

Logic:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df.assign(**{'m': m,
'n': n,
'm | n': m | n
}
)
Index Ingredient Dish m n m | n
0 1 Potato Pie True True True
1 2 Potato Juice True True True
2 3 Potato Fries True False True
3 4 Potato Pure True False True
4 5 Apple Pie False True True
5 6 Apple Juice False True True
6 7 Apple Fries False False False
7 8 Apple Pure False False False
# also possible: 'any': lambda x: x[['m', 'n']].any(axis=1)
</code>
<code>df.assign(**{'m': m, 'n': n, 'm | n': m | n } ) Index Ingredient Dish m n m | n 0 1 Potato Pie True True True 1 2 Potato Juice True True True 2 3 Potato Fries True False True 3 4 Potato Pure True False True 4 5 Apple Pie False True True 5 6 Apple Juice False True True 6 7 Apple Fries False False False 7 8 Apple Pure False False False # also possible: 'any': lambda x: x[['m', 'n']].any(axis=1) </code>
df.assign(**{'m': m, 
             'n': n, 
             'm | n': m | n
             }
          )

   Index Ingredient   Dish      m      n  m | n
0      1     Potato    Pie   True   True   True
1      2     Potato  Juice   True   True   True
2      3     Potato  Fries   True  False   True
3      4     Potato   Pure   True  False   True
4      5      Apple    Pie  False   True   True
5      6      Apple  Juice  False   True   True
6      7      Apple  Fries  False  False  False
7      8      Apple   Pure  False  False  False

# also possible: 'any': lambda x: x[['m', 'n']].any(axis=1)

1

You can select values with Apple if not match list and then invert mask by ~ for expected ouput:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>out = df[ ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice']))]
print (out)
Index Ingredient Dish
0 1 Potato Pie
1 2 Potato Juice
2 3 Potato Fries
3 4 Potato Pure
4 5 Apple Pie
5 6 Apple Juice
</code>
<code>out = df[ ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice']))] print (out) Index Ingredient Dish 0 1 Potato Pie 1 2 Potato Juice 2 3 Potato Fries 3 4 Potato Pure 4 5 Apple Pie 5 6 Apple Juice </code>
out = df[ ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice']))]

print (out)
   Index Ingredient   Dish
0      1     Potato    Pie
1      2     Potato  Juice
2      3     Potato  Fries
3      4     Potato   Pure
4      5      Apple    Pie
5      6      Apple  Juice

How it working:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>print (df.assign(m1=df['Ingredient'].eq('Apple'),
m2 = ~df['Dish'].isin(['Pie','Juice']),
both =(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice'])),
invert= ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice']))))
Index Ingredient Dish m1 m2 both invert
0 1 Potato Pie False False False True
1 2 Potato Juice False False False True
2 3 Potato Fries False True False True
3 4 Potato Pure False True False True
4 5 Apple Pie True False False True
5 6 Apple Juice True False False True
6 7 Apple Fries True True True False
7 8 Apple Pure True True True False
</code>
<code>print (df.assign(m1=df['Ingredient'].eq('Apple'), m2 = ~df['Dish'].isin(['Pie','Juice']), both =(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice'])), invert= ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice'])))) Index Ingredient Dish m1 m2 both invert 0 1 Potato Pie False False False True 1 2 Potato Juice False False False True 2 3 Potato Fries False True False True 3 4 Potato Pure False True False True 4 5 Apple Pie True False False True 5 6 Apple Juice True False False True 6 7 Apple Fries True True True False 7 8 Apple Pure True True True False </code>
print (df.assign(m1=df['Ingredient'].eq('Apple'),
                 m2 = ~df['Dish'].isin(['Pie','Juice']),
                 both =(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice'])),
                 invert= ~(df['Ingredient'].eq('Apple') & ~df['Dish'].isin(['Pie','Juice']))))
   Index Ingredient   Dish     m1     m2   both  invert
0      1     Potato    Pie  False  False  False    True
1      2     Potato  Juice  False  False  False    True
2      3     Potato  Fries  False   True  False    True
3      4     Potato   Pure  False   True  False    True
4      5      Apple    Pie   True  False  False    True
5      6      Apple  Juice   True  False  False    True
6      7      Apple  Fries   True   True   True   False
7      8      Apple   Pure   True   True   True   False

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật