3

I want to compare two columns with value (1) and list rows that satisfy this condition. Here is my code:

import pandas as pd

df = pd.DataFrame({'col':[0,1,1,0,1],
                   'col2':[0,1,0,1,0],
                   'ord':[0,1,2,3,4]
})

df1 = df.loc[df['col'] == 1 & df['col2'] == 1]

print(df1)

Expected output:

   col  col2  ord
0    1     1    1
0

1 Answer 1

4

Add parentheses because priority precedence of & operator:

df1 = df.loc[(df['col'] == 1) & (df['col2'] == 1)]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.