Home > Blockchain >  if column a == value, drop rows where column b equals
if column a == value, drop rows where column b equals

Time:06-12

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

CodePudding user response:

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

CodePudding user response:

What you expect as output:

What I'd like to see is:

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

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

What you explain:

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.

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

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