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

Time:03-12

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 ??

CodePudding user response:

Use boolean slicing. There are many ways:

specific list of target colums

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

or, global filter based on name:

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

or all columns except "output":

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

output:

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

CodePudding user response:

You can do condition filtering the rows

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

CodePudding user response:

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]
  • Related