1

I am trying to grab the rows of a data frame that satisfy one or both of the following boolean statements:

1) df['colName'] == 0
2) df['colName'] == 1

I've tried these, but neither one works (throws errors):

df = df[df['colName']==0 or df['colName']==1]
df = df[df['colName']==0 | df['colName']==1]

Any other ideas?

2 Answers 2

1

you are missing ()

df = df[(df['colName']==0) | (df['colName']==1)]

this will probably raise a copy warning but will still works.

if you don't want the copy warning, use an indexer such has:

indexer = df[(df['colName']==0) | (df['colName']==1)].index
df = df.loc[indexer,:]
Sign up to request clarification or add additional context in comments.

Comments

1

You could clean up what you've done using eq instead of ==

df[df.colName.eq(0) | df.colName.eq(1)]

For this case, I recommend using isin

df[df.colName.isin([0, 1])]

Using query also works but is slower

df.query('colName in [0, 1]')

Timing
isin is quickest on df defined below
df = pd.DataFrame(np.random.randint(3, size=10000), columns=['colName'])

enter image description here

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.