Reputation: 5
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
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
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
Reputation: 825
You can do condition filtering the rows
data[~((data['input-1']==0) | (data['input-2']==0))]
Upvotes: 0