JoseGzz
JoseGzz

Reputation: 25

Why is my OR Pandas filter logic not working?

I'm trying to filter some values from my dataframe but some cases in the output do not seem to follow the filter rules. Could there be a problem with the logic? I want to get entries in which either of the columns fulfill the condition (that's why I use | instead of &).

I've already tried by converting the columns to integers.

    datos[
       ((datos['edad'] >=21) & (datos['edad']<=68)) |
       ((datos['EdadCurp']) >= 21 & (datos['EdadCurp']<=68)) |
       ((datos['edadrfc']) >= 21 & (datos['edadrfc']<=68))
    ][["edad", "EdadCurp", "edadrfc"]].head(100)

enter image description here

For instance, entry #3 does not fulfill any of the conditions but still is part of the output.

Upvotes: 0

Views: 106

Answers (1)

Peque
Peque

Reputation: 14801

You've got a typo in your filter, I think. Try with:

    datos[
       ((datos['edad'] >=21) & (datos['edad']<=68)) |
       ((datos['EdadCurp'] >= 21) & (datos['EdadCurp']<=68)) |
       ((datos['edadrfc'] >= 21) & (datos['edadrfc']<=68))
    ][["edad", "EdadCurp", "edadrfc"]].head(100)

Note that you had (datos['EdadCurp']) >= 21 & ... instead of (datos['EdadCurp'] >= 21) & .... Same with edadrfc.

Upvotes: 2

Related Questions