Reputation: 53786
This code :
# from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import fix_yahoo_finance as yf
%matplotlib inline
import seaborn as sns
sns.set_style("darkgrid")
data = yf.download(tickers = ['AAPL' , 'GOOGL'] , start='2016-01-01',end='2018-01-01')
data
data.columns
returns :
MultiIndex(levels=[['Adj Close', 'Close', 'High', 'Low', 'Open', 'Volume'], ['AAPL', 'GOOGL']],
labels=[[4, 4, 2, 2, 3, 3, 1, 1, 0, 0, 5, 5], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]])
Attempting to select from multi index with :
data.xs('AAPL', level='Adj Close', axis=1)
returns :
/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/multi.py in _get_level_number(self, level)
612 except ValueError:
613 if not isinstance(level, int):
--> 614 raise KeyError('Level %s not found' % str(level))
615 elif level < 0:
616 level += self.nlevels
KeyError: 'Level Adj Close not found'
How to select from a MultiIndex
pandas dataframe ?
Upvotes: 0
Views: 66
Reputation: 862406
I think need tuple
for select by both levels of MultiIndex
:
print (data[('Adj Close', 'AAPL')])
Date
2015-12-31 100.540207
2016-01-04 100.626175
2016-01-05 98.104546
2016-01-06 96.184654
2016-01-07 92.125244
2016-01-08 92.612358
2016-01-11 94.111984
2016-01-12 95.477859
2016-01-13 93.023087
2016-01-14 95.057579
2016-01-15 92.774750
...
For more complicated selections use slicers:
idx = pd.IndexSlice
print (data.loc[:, idx[['Adj Close'], :]].head())
Adj Close
AAPL GOOGL
Date
2015-12-31 100.540207 778.010010
2016-01-04 100.626175 759.440002
2016-01-05 98.104546 761.530029
2016-01-06 96.184654 759.330017
2016-01-07 92.125244 741.000000
Upvotes: 1