I have two dataframes
df
Out[162]:
colA colB
L0 L1 L2
A1 B1 C1 1 2
C2 3 4
B2 C1 5 6
C2 7 8
A2 B3 C1 9 10
C2 11 12
B4 C1 13 14
C2 15 16
df1
Out[166]:
rate
from to
CHF CHF 1.000000
MXN 19.673256
ZAR 0.000000
XAU 0.000775
THB 32.961405
When I did
df.query('L0=="A1" & L2=="C1"')
Out[167]:
colA colB
L0 L1 L2
A1 B1 C1 1 2
B2 C1 5 6
Which give me back the expected out put .
Then I want to apply the same function in df1
df1.query('ilevel_0=="CHF" & ilevel_1=="MXN"')
and
df1.query('from=="CHF" & to=="MXN"')
Both failed
What happened here ?
Data Input :
#df
{'colA': {('A1', 'B1', 'C1'): 1,
('A1', 'B1', 'C2'): 3,
('A1', 'B2', 'C1'): 5,
('A1', 'B2', 'C2'): 7,
('A2', 'B3', 'C1'): 9,
('A2', 'B3', 'C2'): 11,
('A2', 'B4', 'C1'): 13,
('A2', 'B4', 'C2'): 15},
'colB': {('A1', 'B1', 'C1'): 2,
('A1', 'B1', 'C2'): 4,
('A1', 'B2', 'C1'): 6,
('A1', 'B2', 'C2'): 8,
('A2', 'B3', 'C1'): 10,
('A2', 'B3', 'C2'): 12,
('A2', 'B4', 'C1'): 14,
('A2', 'B4', 'C2'): 16}}
#df1
{'rate': {('CHF', 'CHF'): 1.0,
('CHF', 'MXN'): 19.673256,
('CHF', 'THB'): 32.961405,
('CHF', 'XAU'): 0.000775,
('CHF', 'ZAR'): 0.0}}
fromis a reserved keyword in python, so using it inquerydoesn't work.pandas.core.computation.ops.UndefinedVariableError: name 'ilevel_0' is not definedilevel_0is the name given to the axis when it has no name. If the axis already has a name, then you need to use that name in query. On the other hand, if you dodf1.rename_axis([None, None])and then call query, you get your answer.