0

I am trying to select rows in a data frame data based on two conditions. One is on a column called let's call it id is meant to be selected on membership in a list keep and another, call it toggle, is meant to be checked on an equality condition.

My code is:

 new_data = data[(data['id'].isin(keep) & data['toggle']==2)]

which returns a data frame with zero rows. However if I do

new_data = data[data['id'].isin(keep)]
new_data = new_data[new_data['toggle']==2]

I get a data frame with several rows which is what SHOULD happen. What am I doing wrong here?

  • 1
    Try `new_data = data.loc[(data['id'].isin(keep)) & (data['toggle']==2)]` – rafidini Dec 10 '22 at 17:01
  • Use `data[data['id'].isin(keep) & data['toggle'].eq(2)]`, or parentheses, see [operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) – mozway Dec 10 '22 at 17:01

0 Answers0