0

I am using pandas and matplotlib to evaluate an excel sheet that I have converted to a dataframe. However, two of my columns have zeros as values in some of the rows, which I need to remove. We are talking a couple thousand rows for the dataframe total.

The zeros only appear in two of my 8 columns. I'd like to remove any rows that have zeros in one or both of those two columns.

Does anyone know how I may go about doing this? I've tried df.loc[(df!=0).any(1)] but to no avail.

Any and all help would be greatly appreciated.

1 Answers1

1

IIUC, to drop all columns where the column as all zeros, you can use:

df.drop(df.columns[df.eq(0).all()], axis=1)

Update: Where columns A and C are your two checking columns, you can use this:

df.drop(df.index[(df.C.eq(0) | df.A.eq(0))])
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • My apologies, I should have stated my question better. I want to remove the rows with zeros not the columns or perhaps replace the zeros with the mean of that particular column. – Paul Blessing Apr 06 '18 at 16:22
  • The zeros only appear in two of my 8 columns. I'd like to remove any rows that have zeros in one or both of those two columns. – Paul Blessing Apr 06 '18 at 16:23