Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a row in pandas dataframe if multiple columns contains specific value

Tags:

python

pandas

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.

like image 530
coder Avatar asked Oct 14 '25 14:10

coder


2 Answers

Try this:

df = df[~df.filter(like='user').apply(set, axis=1).eq({'pass'})]
like image 184
Nk03 Avatar answered Oct 17 '25 03:10

Nk03


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.

like image 37
Akmal Soliev Avatar answered Oct 17 '25 04:10

Akmal Soliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!