Reputation: 6615
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
Upvotes: 1
Views: 1467
Reputation: 210832
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
Upvotes: 1