Search code examples
pythonpandasdataframedrop

if column a == value, drop rows where column b equals


I am trying to drop rows in my df where SPCD == 104, drop rows where Age >= 950 and for some reason I can't for the life of me figure out how to do it.

dropped_ages = d_age[ (d_age['SPCD'] == 104) & (d_age['Age'] >= 950) ]

This is a line of code I've tried, but it ended up deleting every entry of SPCD 104. I tried it with <= and >= both resulted in the same thing.

So the initial df may look like:

    SPCD   Age
0   104    1100
1   104    300
2   104    950
3   133    200
4   104    400
5   133    100
6   104    1000

What I'd like to see is:

    SPCD   Age
0   104    300
1   104    950
2   133    200
3   104    400
4   133    100

Solution

  • Negate your condition:

    d_age[(d_age["SPCD"] != 104) | (d_age["Age"] < 950)]
    

    This outputs:

       SPCD  Age
    1   104  300
    3   133  200
    4   104  400
    5   133  100