我希望能够根据列表中的列名动态查询每个列名。我可以使用@sign在等号的另一边动态传递,但我真的想动态地做列。
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
答案 0 :(得分: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
动态生成查询:
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