0
$\begingroup$

I have a dataset that like any other has zeros and i need to get rid of them. The problem is that I want to delete rows where all the columns values but the timestamp are zeros

$\endgroup$
1
$\begingroup$

You can use difference to not look at the Timestamp column. Then sum over the horizontal axis looking for non-zero sum rows:

df.loc[df[df.columns.difference(['Timestamp'])].sum(axis=1) != 0]

$\endgroup$
0
$\begingroup$

If you want to remove rows which have only timestamp as null, you can use something like this

df.drop(df[df['Timestamp'].isnull()].index,inplace=True)
$\endgroup$
  • $\begingroup$ I actually want to delete the rows that all their columns are zeros. But the problem is that i have a timestamp column which does not have zeros ( it has normal values ) $\endgroup$ – MOHAMED KOUBAA Sep 12 '19 at 12:03
0
$\begingroup$

If you want to actually remove the rows, you can do that as follows:

# get the columns, you want to check
cols_to_check= [col for col in df.columns if col != 'Timestamp']
# you can also use TitoOrt's method like:
# cols_to_check= df.columns.difference(['Timestamp'])

# build an indexer with one boolean per row that is
# true if all cols_to_check contain zero
all_zero_indexer= (df[cols] == 0).all(axis='columns')

# if it is now sufficient to just work with the 
# subset of the dataframe, you can use
df.loc[~all_zero_indexer]
# in place of df to only see the rows where not all
# columns are zero (just reasign it to variable df)

# if you actually want to delete the zero-rows,
# you can do that with the following line
df.drop(labels=all_zero_indexer.index[all_zero_indexer], inplace=True)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.