0

I have the following DF:

enter image description here

I want to remove all rows whereby TO and GP columns are negative and '0', and remove all rows whereby TTS and BMI columns are positive and '0'.

I have tried creating code like the following for each column however this is also deleting rows that are null which I do not want:

df [df ['GP'] >= 0]

What's the most efficient way to do this?

1
  • df[(df['GP'] >= 0) & (df['GP'] != np.NaN)] and get round the difficulty of & - its caught me out many a time Commented Nov 25, 2020 at 23:22

1 Answer 1

1

Try with df.drop:

df.drop(df[(df.TO <= 0)  | (df.GP <=0) | (df.TS >= 0) | (df.BMI >=0)].index, inplace=True)
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried this but receiving the following error message: TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]. Can you help?
I did some changes, please check again
So this logic only removes a row if both TO and GP are <=0. I want to remove a row if either TO and GP are <=0. From reading my original question I should have explained this clearer so my bad. Thanks for you help
So just changing '&' to '|' resolves this: df.drop(df[((df.TO <= 0) | (df.GP <=0)) | ((df.TS >= 0) | (df.BMI >=0))].index, inplace=True)
Great, i changed my answer too. Please accept the answer if your problem was resolved, Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.