Reputation: 71
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:
Upvotes: 2
Views: 77
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:
Upvotes: 2