1

I want to be able to query each column name dynamically given the column names in a list. I can use the @sign to pass in dynamically on the other side of the equal sign but I'm really trying to do the columns dynamically..

import pandas as pd
import bumpy as np

df = pd.DataFrame(np.random.randn(10, 2), columns=list('ab'))
df

columns = ['a','b']

for each_column in columns:
    df.query("@each_column > 1")  ## error
1
  • even if we provided you with a string to query with, it's unclear what you're after. Your code won't produce any output. What is your desired result? Commented Jun 15, 2017 at 20:00

1 Answer 1

1

IIUC:

In [12]: df
Out[12]:
          a         b
0 -0.272252 -0.467849
1 -0.221294 -2.583866
2 -0.117282  1.044487
3  0.451677 -0.434122
4  0.899038  0.712295
5  1.591961  0.031358
6 -1.398513 -0.600160
7  1.073948  1.348097
8 -1.016790 -0.773364
9 -0.628775  1.116282

dynamically generating a query:

In [13]: q = ' and '.join('{} > 1'.format(c) for c in df.columns)

In [14]: q
Out[14]: 'a > 1 and b > 1'

In [15]: df.query(q)
Out[15]:
          a         b
7  1.073948  1.348097
Sign up to request clarification or add additional context in comments.

1 Comment

not exactly how I was seeing things, but this gives me what I need to solve this. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.