ritzrori
ritzrori

Reputation: 91

looping df.query by setting the condition as a variable

So I have multiple criteria and I want to use a loop to query them each time so I can filter the data. So essentially I want to:

metric = ["case1", "case2"] #etc 
df = df.query('Metric == metric[i]')

#instead of-->

df = df.query('Metric == "case1"')
df = df.query('Metric == "case2"')

metric = ["case1", "case2"] etc 
df = df.query('Metric == metric[i]')
#I will loop back around instead of doing them individually

df = df.query('Metric == "case1"')

df = df.query('Metric == "case2"')

Upvotes: 0

Views: 375

Answers (1)

BENY
BENY

Reputation: 323226

You can do with

df.query('Symbol==@metric[0]')

Upvotes: 1

Related Questions