Home > Mobile >  Delete the rows with all value zero and keep one specific column in dataframe
Delete the rows with all value zero and keep one specific column in dataframe

Time:02-26

I have a dataframe, I want to delete the rows with all zero. However, the first column is the id and I want to keep that column. I check with

df  = df[(df.T != 0).any()]

however, it delete all the column

import pandas as pd
import numpy as np
df = pd.DataFrame()

df['id'] = [ 'a',  'b', 5, 'd' ]
df['b'] = [ 0, 9, 0, 2]
df['c'] = [ 0, 2, 0, 2]
df['d'] = [ 0, 7, 0, 5]

Here is the new DataFrame which I want.

enter image description here

CodePudding user response:

You could create a boolean mask that returns True if none of the non-"id" column values are 0 for each row, False otherwise:

out = df[df.drop(columns='id').ne(0).all(axis=1)]

Output:

  id  b  c  d
1  b  9  2  7
3  d  2  2  5

CodePudding user response:

Try with sum all 0 if the result less than 3 then we keep it

df[df.eq(0).sum(1)<3]
Out[438]: 
  id  b  c  d
1  b  9  2  7
3  d  2  2  5
  • Related