1

I know i can make a new df while dropping by criterias: df = df[df.ID != 0]

But what if i want to use more criterias. I want to take out all rows that satisfy condition A and B. An example:

        for v in range(len(drop)-1):
            df = df[df.town != drop.at[v, 0] and df.state != drop.at[v, 1]]

drop is just a df with the names of towns and states, which should drop if in one row bot conditions are true.

I get this Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can anyone help me?

Tim
  • 13
  • 2
  • This is a duplicate question. See [this](https://stackoverflow.com/questions/22591174/pandas-multiple-conditions-while-indexing-data-frame-unexpected-behavior) – Shir Mar 05 '21 at 09:54
  • got it, thank you! – Tim Mar 05 '21 at 10:05

2 Answers2

0

FYI, Logical operators for boolean indexing in Pandas.


Try this:

criteria_1 = df.town != drop.at[v, 0]
criteria_2 = df.state != drop.at[v, 1]

criteria = criteria_1 & criteria_2

df_db = df_db[criteria]
Jaybe Park
  • 172
  • 1
  • 5
  • Thanks for youre answeres. Can it be, that this will drop all rows with criteria 1 and criteria 2? I want to drop the rows only, if criteria 1 and criteria 2 are true for the same row. – Tim Mar 05 '21 at 10:02
  • got it, thanks all. i used the wrong operator. – Tim Mar 05 '21 at 10:06
0

Pandas require you to split your conditions into boolean vectors, then merge them with a boolean operator (&, | and not and, or)

This should fix your problem :

df = df[(df.town != drop.at[v, 0]) & (df.state != drop.at[v, 1])]

Documentation

arhr
  • 1,505
  • 8
  • 16
  • Thanks for youre answeres. Can it be, that this will drop all rows with criteria 1 and criteria 2? I want to drop the rows only, if criteria 1 and criteria 2 are true for the same row. – Tim Mar 05 '21 at 10:02
  • got it, thanks all. i used the wrong operator. – Tim Mar 05 '21 at 10:06
  • Here the `&` means both conditions, for a row, needs to be validated. If you use `|` any of the conditions will be sufficient – arhr Mar 05 '21 at 10:07