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
Soufiane Sabiri
  • 749
  • 1
  • 5
  • 20

1 Answers1

4

Add parentheses because priority precedence of & operator:

df1 = df.loc[(df['col'] == 1) & (df['col2'] == 1)]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252