Home > database >  Pandas loc by list
Pandas loc by list

Time:08-24

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’]]

CodePudding user response:

No, but you can do:

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

CodePudding user response:

Yeah, you can do something like:

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

CodePudding user response:

Check with tuple

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