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.
Try this:
df = df[~df.filter(like='user').apply(set, axis=1).eq({'pass'})]
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)
df[(df['one'] != 'pass') & (df['two'] != 'pass')]
as you can see you can pass multiple conditions for your indexation in Pandas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With