jmark7453
jmark7453

Reputation: 71

conditionally dropping rows from a pandas dataframe

I'm trying to conditionally drop rows of from a pandas dataframe and running into trouble. For example, I want to drop the second and third rows from the dataframe in the example below.

import pandas as pd

df = pd.DataFrame([
    [12, 10, 7, 0],
    [11, 0, 0, 3],
    [0, 0, 0, 0],
    [0, 6, 7, 5],
    [4, 11, 3,4],
    [5, 2, 5, 0]],
    columns=["num1", "num2","num3","num3"])

I've tried:

df.loc[~(df['num2','num3']==0).all(axis=1)]

but get the error "TypeError: only integer scalar arrays can be converted to a scalar index"

I've also tried this per a suggestion below and gotten unexpected output: enter image description here

Upvotes: 2

Views: 77

Answers (2)

Space Impact
Space Impact

Reputation: 13255

Use ne with all as:

df = df.loc[df[['num2','num3']].ne(0).all(axis=1)]
print(df)
   num1  num2  num3  num4
0    12    10     7     0
3     0     6     7     5
4     4    11     3     4
5     5     2     5     0

Here is the screenshot of my execution:

Output

Upvotes: 2

rhug123
rhug123

Reputation: 8768

Try

df.loc[(df['num2']!=0) & (df['num2']!=0)]

Upvotes: 2

Related Questions