Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

How do I drop a row if any of the values in the row equal zero?

I would normally use df.dropna() for NaN values but not sure how to do it with "0" values.

like image 847
azuric Avatar asked Nov 19 '14 15:11

azuric


People also ask

How do I delete rows in pandas DataFrame based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do you remove a row that has any zero values?

You can use the Filter function to filter out all rows based on the zero values in a certain column, and then delete all visible rows later. Please do as follows. 1. Select the column cells which contain the zero values you want to delete the entire rows based on, then click Data > Filter.

How do you drop a row with a certain value?

One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).


1 Answers

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

df[(df != 0).all(1)] 
like image 160
acushner Avatar answered Sep 17 '22 13:09

acushner