Hasibul Hasan Rupok
Hasibul Hasan Rupok

Reputation: 5

How can i remove a row if any input value of a row contains 0, output can be 0 or 1

I have a dataset like this

input-1 input-2 output
45 0 1
50 99 0
0 45 0
5 9 1
0 0 1

now i want to remove all the row which any input column contains 0, if input is not 0 but output is zero its okay i do not delete that row. After deleting my new dataset looks like this:

input-1 input-2 output
50 99 0
5 9 1

How can i do this in python ??

Upvotes: 1

Views: 88

Answers (3)

mozway
mozway

Reputation: 261820

Use boolean slicing. There are many ways:

specific list of target colums

df[df[['input-1', 'input-2']].ne(0).all(axis=1)]

or, global filter based on name:

df[df.filter(like='input').ne(0).all(axis=1)]

or all columns except "output":

df[df.drop(columns='output').ne(0).all(axis=1)]

output:

   input-1  input-2  output
1       50       99       0
3        5        9       1

Upvotes: 2

Ch3steR
Ch3steR

Reputation: 20669

You can use DataFrame.query to query your dataframe using boolean expression.

df.query('`input-1` != 0 and `input-2` != 0')

   input-1  input-2  output
1       50       99       0
3        5        9       1

The above is equivalent to:

m = df['input-1'].ne(0) & df['input-2'].ne(0)
df[m]

Upvotes: 0

Pawan Jain
Pawan Jain

Reputation: 825

You can do condition filtering the rows

data[~((data['input-1']==0) | (data['input-2']==0))]

Upvotes: 0

Related Questions