Drop Rows With All Zeros in Pandas Data Frame

Drop row in pandas dataframe if any value in the row equals zero

i think the easiest way is looking at rows where all values are not equal to 0:

df[(df != 0).all(1)]

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

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

drop all rows when all cells are either zero or nan

Your solution should be changed by select all columns without first with DataFrame.all:

df1 = df.iloc[:, 1:]
df2 = df[(df1 != 0).all(axis=1) | (df1 != 'nan').all(axis=1)]

Working like DataFrame.isin:

df2 = df[~df.iloc[:, 1:].isin([0, 'nan']).all(axis=1)]

If NaN are missing values is necessary test them by DataFrame.notna:

df1 = df.iloc[:, 1:]
df2 = df[(df1 != 0).all(axis=1) | df1.notna().all(axis=1)]


df2 = df[~df.iloc[:, 1:].fillna('nan').isin([0, 'nan']).all(axis=1)]

Drop all rows in pandas dataframe that are all only 0 or NA

IIUC, you need to wrap the invert under parenthesis :


Pandas dataframe drop rows which store certain number of zeros in it

This will work:

drop_indexs = []
for i in range(len(df.iloc[:,0])):
if (df.iloc[i,:]==0).sum()>=4: # 4 is how many zeros should row min have
updated_df = df.drop(drop_indexs)

Pandas - delete rows with all zeros and delete columns using index of deleted rows

You can try

idx = df.index[(df==0.0).all(axis=1)]
out = df.drop(idx,axis=1).drop(idx)

Related Topics

Leave a reply