Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从多索引中选择?_Python_Pandas - Fatal编程技术网

Python 如何从多索引中选择?

Python 如何从多索引中选择?,python,pandas,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(ti

此代码:

# 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]])
/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'
正在尝试使用以下选项从多个索引中进行选择:

data.xs('AAPL', level='Adj Close', axis=1)
返回:

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]])
/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'
/opt/conda/lib/python3.6/site-packages/pandas/core/index/multi.py in\u get\u level\u number(self,level)
612除值错误外:
613如果不存在(级别,int):
-->614 raise KeyError('找不到级别%s'%str(级别))
615 elif水平<0:
616级+=自我水平
KeyError:“找不到级别调整关闭”

如何从一个
多索引
数据帧中进行选择?

我认为需要
元组
进行两级
多索引
的选择:

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