Pandas 系列到数据帧

Pandas 系列到数据帧,pandas,Pandas,我有一个系列,看起来像这样: Adj Close Date minor 2017-09-22 AFK 23.500000 ASHR 29.530001 ECH 49.259998 EGPT 28.139999 EIDO 26.950001 也就是说,对于几个ETF,我每天都在调整收盘价 我想将

我有一个系列,看起来像这样:

                     Adj Close
Date        minor   
2017-09-22  AFK      23.500000
            ASHR     29.530001
            ECH      49.259998
            EGPT     28.139999
            EIDO     26.950001
也就是说,对于几个ETF,我每天都在调整收盘价

我想将其转换为数据帧sush,如下所示:

              AFK         ASHR       ECH
Date            
2017-09-22    23.500000   29.530001  49.259998 ...
2017-09-23    ...
我已尝试使用pivot():

但我收到一条错误消息

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Users\USUARIO\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2392             try:
-> 2393                 return self._engine.get_loc(key)
   2394             except KeyError:


KeyError: 'Date'

During handling of the above exception, another exception occurred:

 ...

KeyError: 'Date'
我做错了什么?

我想您需要通过使用
[]
索引来选择列,以获得系列:

df1 = df['Adj Close'].unstack()
print (df1)
minor        AFK       ASHR        ECH       EGPT       EIDO
Date                                                        
2017-09-22  23.5  29.530001  49.259998  28.139999  26.950001
如果需要,请使用
pivot
首先通过以下方式从
多索引创建列:


其基于ohk sir unstack的解决方案非常棒。;)
df1 = df['Adj Close'].unstack()
print (df1)
minor        AFK       ASHR        ECH       EGPT       EIDO
Date                                                        
2017-09-22  23.5  29.530001  49.259998  28.139999  26.950001
print (df.reset_index())
         Date minor  Adj Close
0  2017-09-22   AFK  23.500000
1  2017-09-22  ASHR  29.530001
2  2017-09-22   ECH  49.259998
3  2017-09-22  EGPT  28.139999
4  2017-09-22  EIDO  26.950001

df1 = df.reset_index().pivot(index='Date', columns='minor', values='Adj Close')
print (df1)
minor        AFK       ASHR        ECH       EGPT       EIDO
Date                                                        
2017-09-22  23.5  29.530001  49.259998  28.139999  26.950001