0

I have a datafram df, and i want to get the rows that a column value equals 1 b 0 c 0 and d 0

df_result = df[df.a == 1 and df.b == 0 and df.c == 0 and df.d == 0]

It says ; The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

SeaBean
  • 21,029
  • 3
  • 10
  • 23
Sevval Kahraman
  • 948
  • 3
  • 8
  • 25

1 Answers1

3

Use & instead of and and put brackets around each value test:

df_result = df[(df.a == 1) & (df.b == 0) & (df.c == 0) & (df.d == 0)]

Alternatively, to avoid using extra brackets, you can use .eq():

df_result = df[df.a.eq(1) & df.b.eq(0) & df.c.eq(0) & df.d.eq(0)]
SeaBean
  • 21,029
  • 3
  • 10
  • 23
  • 1
    or query, check dupe. – jezrael Jun 21 '21 at 07:21
  • Thank you, i want to drop this rows from data set what should i do? – Sevval Kahraman Jun 21 '21 at 07:43
  • 1
    @ŞevvalKahraman If you want to drop these rows instead of selecting these rows, you can use the negation operator `~` to form a negation of all the conditions, e.g. `df_result_drop = df[~((df.a == 1) & (df.b == 0) & (df.c == 0) & (df.d == 0))]` Please remember also to accept the answer by clicking ✓ on the left (see [How to accept SO answers](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)) – SeaBean Jun 21 '21 at 07:48