GeorgeLPerkins
GeorgeLPerkins

Reputation: 1146

Pandas loc by list

I’m not anywhere near a computer right now so I can’t test this out, but can I .loc by a list?

As in, I regularly do

df.loc[df[‘col’] == ‘this’]

Can I do

df.loc[df[[‘col1’, ‘col2’]] == [‘this’, ‘that’]]

Upvotes: 0

Views: 651

Answers (3)

BENY
BENY

Reputation: 323266

Check with tuple

df.loc[df[['col1', 'col2']].apply(tuple,1) == ('this', 'that')]

Upvotes: 0

BeRT2me
BeRT2me

Reputation: 13242

Yeah, you can do something like:

df.loc[df[['col1', 'col2']].eq(['this', 'that']).all(axis=1)]

Upvotes: 1

Tim Roberts
Tim Roberts

Reputation: 54698

No, but you can do:

df.loc[df['col1'] == 'this' & df['col2'] == 'that']

Upvotes: 0

Related Questions