I'm trying to use query with a MultiIndex that has multiple levels of columns.
!pip install pandas-datareader --quiet
Next ...
from pandas_datareader import DataReader
df = DataReader(["SPY", "XOM"], "yahoo", datetime(2012,7,1), datetime(2018,7,21))
df.keys()
Returns ...
MultiIndex(levels=[['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'], ['SPY', 'XOM']],
labels=[[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]],
names=['Attributes', 'Symbols'])
And
df['High']['SPY'].head()
Returns ...
Date
2012-07-02 136.649994
2012-07-03 137.509995
2012-07-05 137.800003
2012-07-06 135.770004
2012-07-09 135.570007
Name: SPY, dtype: float64
I was wondering how to use query with multiple levels? I was thinking something like this?
df.query('High.SPY > 137')