0

Given a data frame, I would like to keep the rows where a given column value does not match the given strings.

For instance, if the column 'En' does not match 'x1', I will keep those rows. I use the following code to do it.

df1 = df1.loc[df1['En'] != 'x1']

If instead of x1 only, there are x1and x2 need to be examined. In other words, I will only keep the rows whose 'En' column does not match either x1 or x2. What's the most efficient way to do that.

This is how I did

 df1 = df1.loc[df1['En'] != 'x1']
 df1 = df1.loc[df1['En'] != 'x2']
2
  • Is ‘En’ a column header Commented Aug 22, 2020 at 4:55
  • You can use isin with an invert ~ for multiple values: df[~df1['En'].isin(['x1','x2',other_values])] Commented Aug 22, 2020 at 5:55

2 Answers 2

1

Use logical AND operator :

df1 = df1.loc[(df1['En'] != 'x1') & (df1['En'] != 'x2')]
Sign up to request clarification or add additional context in comments.

1 Comment

didn't notice that you had already answered when I posted mine. Sorry about that. Upvoted your answer.
1

Are you looking for something like this?

import pandas as pd
df1 = pd.DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],
                    'b' : ['x', 'y', 'z', 'x', 'y', 'x', 'z'],
                    'c' : [1,2,3,4,5,6,7]})
print(df1)

df2 = df1.loc[(df1['b'] != 'x') & (df1['b'] != 'y') ]

print  (df2)

If df1 is :

       a  b  c
0    one  x  1
1    one  y  2
2    two  z  3
3  three  x  4
4    two  y  5
5    one  x  6
6    six  z  7

then df2 will be:

     a  b  c
2  two  z  3
6  six  z  7

An alternate way to do this is using query.

df2 = df1.query("b != 'x' & b != 'y'")

OR

df2 = df1.query("b != ['x','y']")

This will also give you the same result.

For more information about using some of these operators, see https://pandas.pydata.org/pandas-docs/version/0.13.1/indexing.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.