2

I have some DataFrame:

df = pd.DataFrame({'name': ['apple1', 'apple2', 'apple3', 'apple4', 'orange1', 'orange2', 'orange3', 'orange4'], 
                   'A': [0, 0, 0, 0, 0, 0 ,0, 0], 
                  'B': [0.10, -0.15, 0.25, -0.55, 0.50, -0.51, 0.70, 0], 
                  'C': [0, 0, 0.25, -0.55, 0.50, -0.51, 0.70, 0.90],
                  'D': [0.10, -0.15, 0.25, 0, 0.50, -0.51, 0.70, 0.90]})
df
    name    A   B   C   D
0   apple1  0   0.10    0.00    0.10
1   apple2  0   -0.15   0.00    -0.15
2   apple3  0   0.25    0.25    0.25
3   apple4  0   -0.55   -0.55   0.00
4   orange1 0   0.50    0.50    0.50
5   orange2 0   -0.51   -0.51   -0.51
6   orange3 0   0.70    0.70    0.70
7   orange4 0   0.00    0.90    0.90

I'd like to drop all rows that have two or more zeros in columns A,B,C,D.

This DataFrame has other columns that have zeros; I only want to check for zeros in columns A,B,C,D.

1 Answer 1

4

You can use .eq to check if dataframe is equal to 0 and then take sum on axis=1 and return a boolean series by checking if the sum is greater than or equal to 2 (ge):

df[~df[['A','B','C','D']].eq(0).sum(1).ge(2)]

    name    A   B   C   D
2   apple3  0   0.25    0.25    0.25
4   orange1 0   0.50    0.50    0.50
5   orange2 0   -0.51   -0.51   -0.51
6   orange3 0   0.70    0.70    0.70
Sign up to request clarification or add additional context in comments.

2 Comments

This works. Only comment would be to remove these rows: df[~df[['A','B','C','D']].eq(0).sum(1).ge(2)]
@BuffaloCollector ahh okay, yes :) Thank you I missed the part where they had to be dropped

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.