-2

I have the following example DataFrame:

df = pd.DataFrame({'col1': ['A', 'A', 'B', 'A', 'B', 'B', 'A', 'B', 'B', 'A'], 'col2': [0, 0, 1, 1, 0, 1, 0, 0, 0, 0]})

Now, I would like to create a second DataFrame containing only the rows from df where:

col1 - A matches col2: 0 and col1 - B matches col2: 1

df2 = pandasDataFrame({'col1' : ['A', 'A', 'B', 'B', 'A', 'A'], 'col2' : [0, 0, 1, 1, 0, 0]})

I have tried with query, but it is only working on one condition, if I separate condition pair in parenthesis, I get an empty DataFrame.

1 Answers1

2

You could use create a boolean mask and filter:

out = df[(df['col1'].eq('A')&df['col2'].eq(0)) | (df['col1'].eq('B') & df['col2'].eq(1))]

Output:

  col1  col2
0    A     0
1    A     0
2    B     1
5    B     1
6    A     0
9    A     0