4

I've tried looking for an answer to this question but haven't found anything that works for me. I want to remove rows in which certain columns contain specific values. In my case, I want to remove rows where each column that starts with "user" in that row contains the value "pass". E.g. if I have the following dataframe:

idx   start   end  user1   user2 
0     1       3    pass     pass   
1     4       5   burger   burger 
2     6       7   nuggets    cola 
3     10     15     pass    burger

I'd like to remove row 0, but not row 3 as only one of the columns contains the value "pass". Would appreciate any help!

Update: The names and amount of user columns could increase, e.g. there could be user1, user2, user3 and so on.

4 Answers 4

7

Try this:

df = df[~df.filter(like='user').apply(set, axis=1).eq({'pass'})]
Sign up to request clarification or add additional context in comments.

Comments

3
x = np.array([[0,1,2], [3,2,5], [6,2,9], ['pass', 'pass', 1], [1, 2, 'pass']])

df = pd.DataFrame(x, columns=['one', 'two', 'three'])
display(df)

enter image description here

df[(df['one'] != 'pass') & (df['two'] != 'pass')]

enter image description here

as you can see you can pass multiple conditions for your indexation in Pandas.

Comments

1
df = df.drop((df.loc[(df['user1'] == 'pass') && df['user2'] == 'pass']).index)

Comments

0

You can slice the DataFrame with conditionals.

df[(df['user1'] != 'pass') & (df['user2'] != 'pass')]

If you want to make this the new df just use:

df = df[(df['user1'] != 'pass') & (df['user2'] != 'pass')]

1 Comment

Sorry, forgot to mention: the names and amount of user columns could increase :) e.g. there could be user1, user2, user3 and so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.