pmillerhk
pmillerhk

Reputation: 171

Pandas delete a row in a dataframe based on a value

I want do delete rows in a pandas dataframe where a the second column = 0

So this ...

  Code  Int
0    A    0
1    A    1
2    B    1

Would turn into this ...

  Code  Int
0    A    1
1    B    1

Any help greatly appreciated!

Upvotes: 3

Views: 19247

Answers (3)

Tyler Houssian
Tyler Houssian

Reputation: 465

You could use this as well!

df = df[df.Int != 0]

Upvotes: 1

anthidp
anthidp

Reputation: 24

You could use loc and drop in one line of code.

df = df.drop(df["Int"].loc[df["Int"]==0].index)

Upvotes: 1

Hsgao
Hsgao

Reputation: 563

Find the row you want to delete, and use drop.

delete_row = df[df["Int"]==0].index
df = df.drop(delete_row)
print(df)
Code    Int
1   A   1
2   B   1

Further more. you can use iloc to find the row, if you know the position of the column

delete_row = df[df.iloc[:,1]==0].index
df = df.drop(delete_row)

Upvotes: 11

Related Questions