tbone
tbone

Reputation: 1324

Drop all rows in all columns in pandas dataframe if the value in the column row is zero

I want to drop all rows that are zero in the "feet" column.

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


dataset.info()

the above code gives such a result:

col1 8640 non-value object
col2 8640 non-value object
col3 8640 non-value object
col4 8640 non-value object
feet 7640 non-value object

As you can see, the code correctly remove the values ​​in the 'feet' column, but I also want it to delete the rows in all columns where 'feet' = 0

I can do it easily with Numpy but I want to know how it can be done without it.

Upvotes: 1

Views: 313

Answers (2)

jezrael
jezrael

Reputation: 863401

You need boolean indexing:

df1 = df[df['feet'] != 0]

Or DataFrame.query:

df1 = df.query("feet != 0")

Upvotes: 2

ansev
ansev

Reputation: 30930

use this:

df[df['feet'].ne(0)]

or

df[df['feet'] != 0]

or

df[~(df['feet'] == 0)]

df[~(df['feet'].eq(0))]

Upvotes: 1

Related Questions