I want to exclude rows by specifying multiple values for the same column. I have the following dataframe:
X | Y |
---|---|
A | 1 |
B | 2 |
C | 1 |
D | 1 |
B | 1 |
A | 1 |
want to exclude the rows taking values A & B.
Tried the following code:
new_df = df[~(df['X'] == 'A') & ~(df['X'] == 'B')]
its not working, the new dataframe has 0 rows
CodePudding user response:
Use ~
df[(~df.X.isin(['A','B']))]
Out[183]:
X Y
2 C 1
3 D 1
CodePudding user response:
Or you can use pd.DataFrame.query
, coming from the SQL world this make make a little more sense and easier to read.
df.query("X not in ['A','B']")
Output:
X Y
2 C 1
3 D 1
CodePudding user response:
You can even try same logic using lambda
Code:
df[~df.apply(lambda row: row.x in ['A','B'], axis=1)]