如何从熊猫MultiIndex中选择?

时间:2018-07-04 10:44:42

标签: python pandas

此代码:

# 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返回:

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]])

尝试使用

从多索引中进行选择
data.xs('AAPL', level='Adj Close', axis=1)

返回:

/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'

如何从MultiIndex大熊猫数据框中选择?

1 个答案:

答案 0 :(得分:1)

我认为需要tuple来同时进行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
...

对于更复杂的选择,请使用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