5

A part of the answer can be found here (How to select rows from a DataFrame based on column values?), however it's only for one column. I'm wondering how to apply it for paired values in multiple columns (two in this case).

I have a data frame df where the filtering columns are B and C (NaN represents empty cells):

    A     B      C  D
0   1  Blue  Green  4
1   2  Blue  Green  6
2   3  Blue  Green  2
3   4  Blue    NaN  6
4   5  Blue    NaN  9
5   6   NaN  Green  8
6   7  Blue  Green  8
7   8   NaN    NaN  9
8   9   NaN  Green  1
9  10   NaN  Green  2

I would only like to keep the rows with respect to B and C for which B='Blue' and C='Green', all the other rows can be dropped, ideally producing:

    A     B      C  D
0   1  Blue  Green  4
1   2  Blue  Green  6
2   3  Blue  Green  2
6   7  Blue  Green  8

Any ideas?

Code for the dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame({"A": [1,2,3,4,5,6,7,8,9,10], "B": ['Blue', 'Blue', 'Blue', 'Blue','Blue', np.nan, 
'Blue', np.nan, np.nan, np.nan], "C": ['Green', 'Green', 'Green', np.nan, np.nan, 'Green', 'Green', 
np.nan, 'Green', 'Green'], "D": [4,6,2,6,9,8,8,9,1,2]})
print(df)
Matthi9000
  • 1,156
  • 3
  • 16
  • 32

1 Answers1

9

I think what you need is the & operator:

df[(df['B']=='Blue') & (df['C']=='Green')]
Diego Mora Cespedes
  • 3,605
  • 5
  • 26
  • 33