r/learnpython • • 2d ago

Hot write "Not A, unless B" in Boolean Algebra?

I have a rather complex row selection mission at hand.

To select the rows I want, I have to use two lists of keywords. One contains words that I want (kw_Y), and the other contains words I don't want (kw_N). I originally used this simply to collect the rows that have the wanted information, but now I need to do some comparison analysis with rest of the data.

I need to select rows "that don't have the kw_Y terms, unless they have kw_N terms". Or as Google put it: "No A unless B" (meaning A is false except when B is true) translates to ¬A unless B, or ¬A ∨ B (logically equivalent to B → A, "if B then A").

Boolean logic has never been my strongest point. What would this be in Python?

Codes:

good_stuff = dataframe[kw_Y & ~kw_N] #Yes these masks, not just lists. Algebra is the focus.

bad_stuff = dataframe[kw_N & ~kw_Y] #didn't select enough rows

bad_stuff2 = dataframe[~kw_Y] #I realized this removes rows that contain stuff I now want. 

all_bad_stuff = dataframe[???]
3 Upvotes

20 comments sorted by

10

u/Free-Good9203 2d ago

Since you want the exact "rest of the data" (everything not in `good_stuff`), De Morgan's law makes this super clean:

The opposite of `kw_Y & ~kw_N` is `~kw_Y | kw_N` (use bitwise `|`, not `or`, since these are pandas boolean masks—`or` will throw an ambiguous truth value error).

Honestly though, the easiest way to avoid messing up the algebra (and guarantee zero overlap or dropped rows) is to just store the mask once and invert it:

is_good = kw_Y & ~kw_N

good_stuff = dataframe[is_good]

all_bad_stuff = dataframe[~is_good] # exact same result as dataframe[~kw_Y | kw_N]

If you want to double-check, `len(good_stuff) + len(all_bad_stuff) == len(dataframe)` will always be `True` (assuming no `NaN`s in the masks).

1

u/Dragoran21 2d ago

I tested and ”|” was what I needed.  Thank you!

2

u/codeguru42 2d ago

This is because you are doing boolean logic with pandas. For an if statement you would use or. Your ai assistant will better explain the difference than I can. In the future, be sure to mention that you are using pandas and it will give the correct syntax.

1

u/Free-Good9203 2d ago

glad that worked! pandas bitwise ops trip everyone up at least once

7

u/ExpertSentence4171 2d ago

Didn't you say it yourself? [~kw_Y or kw_N]? Maybe I'm misunderstanding the problem here

1

u/Dragoran21 2d ago

I used to want ”meatball” and not ”meat factory” but no it is the opposite.

1

u/socal_nerdtastic 2d ago

Seems correct to me. What are the kw_Y and kw_N in your meat example?

1

u/Dragoran21 2d ago

Y:meat, N:factory

2

u/socal_nerdtastic 2d ago edited 2d ago

I take it from your other comment that the real problem is you didn't realize the difference between or and | (both pronounced "or"). Classic XY problem. FWIW in the future include a small example of code and data that we can run to demonstrate your issue; we would have seen this error much faster.

1

u/codeguru42 2d ago

Not to mention throwing nympy or pandas into the mix

1

u/Financial_Farm4326 2d ago

yeah i read it the same way, they literally wrote out the logic in the post already

5

u/Mandelbrots-dream 2d ago

I believe you'll need

(not A) or B

Here's the truth table:

ABnot A(not A) or B
TTFT
TFFF
FTTT
FFTT

2

u/SharkSymphony 2d ago

That's how I would encode it: `B | (~A)`.

1

u/kilkil 2d ago

using the word "unless" might be adding extra confusion here. the typically accepted term is "or". so what you are looking for is "(not A) or B". that's actually how you write it in Python directly:

good_rows = []
for row in rows:
    A = has_kw_Y(row)
    B = has_kw_N(row)
    if (not A) or B:
        good_rows.append(row)

now in your code snippets you're using bitwise operators for some reason. Assuming this is intended, and you understand the difference between bitwise operators and boolean operators, you would also write it the same way (not A or B):

good_stuff = dataframe[(~kw_Y) | kw_N]

2

u/Dragoran21 2d ago

Don't select rows containing "food" unless it is "dog food", for example.

1

u/kilkil 2d ago

for your own understanding of boolean logic it would probably be helpful to try to think, "how can I rephrase this condition to only use the words 'and', 'or', 'not'". not even in code, just in plain english.

for example I would reword your sentence as:

Select rows that either do not contain "food", or do contain "dog food".

this helps us translate to code 1-to-1:

def should_select_row(row):
    return (not has_food(row)) or has_dog_food(row)

good_rows = list(filter(should_select_row, rows))

5

u/jameyiguess 2d ago

Isn't that just A and B

6

u/funkdefied 2d ago

(A and B) or (not A)

2

u/Some-Passenger4219 2d ago

Make a table. One row for A & B both true, one for both false, one for A only, one for B only. Which gives you the results you want? Compare it to the ops you already know.

1

u/pythonwiz 1d ago

I suggest restating that as “unless b, a”; then make a truth table, fill out the entries, and compare to other binary operators. That should make it clear. You could also try other ways of restating that in English and see if anything clicks for you.