user11509999
user11509999

Reputation:

Pandas DataFrame:Remove rows based on condition

I have df with columns roll , name and present. I'm trying to remove data where name == marcus and present == False.

        roll    name    present
    0    123     bruno     False
    1    123     bruno     False
    2    123     bruno     True
    3    78      marcus    False       (Remove this row)
    4    78      marcus    True
    5    78      marcus    True

Output:

        roll    name    present
    0    123     bruno     False
    1    123     bruno     False
    2    123     bruno     True
    4    78      marcus    True
    5    78      marcus    True

Is it possible to remove that row without splitting tht dataframe

Upvotes: 1

Views: 809

Answers (3)

Ajay A
Ajay A

Reputation: 1068

Try this

df = df[~((df.name == 'marcus') & (df.present == False))]

Upvotes: 1

Chandu Siddi
Chandu Siddi

Reputation: 16

Please try this.

df = df[!((df['name'] == 'marcus') && (df['present'] == False))]

Upvotes: 0

Jan Willem
Jan Willem

Reputation: 1094

Just do a selection:

df = df[(df['name'] != 'marcus') & (df['present'] != False)]

Remember to check for where name NOT equals marcus and present NOT equals False, because that is what you want to keep!

Upvotes: 0

Related Questions