Filter rows based on two columns together

Viewed 880

I am trying to filter out rows based on two rows values. Most of the questions' solution that I see use the following approach:

df.loc[(df['A'] != 'yes') & (df['B'] != 'no')]

This filters the rows with a A and B different than one value, what I want to do is to filter rows where the columns have the values I am filtering, example:

Player | action | result
1          A        B
2          B        A
3          C        A
4          A        B
5          A        C

In this example I want to remove rows that have action A and result B. Using the example above it would remove actions equal to A and rows with result equal to B. I want to remove actions A that have result B.

Output expected:

  Player | action | result
    2          B        A
    3          C        A
    5          A        C

Probably I am making a lot of confusion here and this is straightforward. Anyhow, any help would be appreciated!

Regards

2 Answers

Could you please try following.

import pandas as pd
df2[~((df2["action"]=='A') & (df2["result"]=='B'))]

Output of data frame will be as follows.

  Player action result
1   2    B    A
2   3    C    A
4   5    A    C

I think this is what you want

pd.concat([df[(df['action'] == 'A') & (df['result'] != 'B')],df[(df['action'] != 'A')]])
Related