Mari
Mari

Reputation: 698

Delete rows with all the zeros elements in all columns exceptionally leaving a single non zero column in pandas DF

I have a pandas Df with 2 million rows *10 columns. I want to delete all the zero elements in a row for all columns except single column with non zero elements.

Ex. My Df like:

Índex   Time    a   b   c   d   e
0       1       0   0   0   0   0
1       2       1   2   0   0   0 
2       3       0   0   0   0   0
3       4       5   0   0   0   0  
4       5       0   0   0   0   0   
5       6       7   0   0   0   0 

What I needed:

Índex   Time        a   b   c   d   e
    0       2       1   2   0   0   0 
    1       4       5   0   0   0   0  
    2       6       7   0   0   0   0

My Requirement:
Requirement 1:
Leaving the 1st column (Time) it should check for zero elements in every rows. If all column values are zero delete that particular row.
Requirement 2:
Finally I want my Index to be updated properly

What I tried:
I have been looking at this link.
I understood the logic used but I wasn't able to reproduce the result for my requirement.

I hope there will be a simple method to do the operation...

Upvotes: 1

Views: 1171

Answers (1)

jezrael
jezrael

Reputation: 863401

Use iloc for select all columns without first, comapre for not equal by ne and test at least one True per rows by any for filter by boolean indexing, last reset_index:

df = df[df.iloc[:, 1:].ne(0).any(axis=1)].reset_index(drop=True)

Alternative with remove column Time:

df = df[df.drop('Time', axis=1).ne(0).any(axis=1)].reset_index(drop=True)

print (df)
   Time  a  b  c  d  e
0     2  1  2  0  0  0
1     4  5  0  0  0  0
2     6  7  0  0  0  0

Upvotes: 4

Related Questions