Python 将面板转换为具有(次要、项目)多级列的DataFrame?

Python 将面板转换为具有(次要、项目)多级列的DataFrame?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个熊猫小组: Items axis: X1 to X3 Major_axis axis: (1973-09-30 00:00:00, 1989-03-31 00:00:00) to (2015-07-31 00:00:00, 2015-08-21 00:00:00) Minor_axis axis: A to C 我想把它转换成一个数据帧,它有一个多级列(Item,Minor),多级列如下所示: mi_tuples = [ ('A','X1'), ('A','X2'), ('A','X3

我有一个熊猫小组:

Items axis: X1 to X3
Major_axis axis: (1973-09-30 00:00:00, 1989-03-31 00:00:00) to (2015-07-31 00:00:00, 2015-08-21 00:00:00)
Minor_axis axis: A to C
我想把它转换成一个数据帧,它有一个多级列(Item,Minor),多级列如下所示:

mi_tuples = [ ('A','X1'), ('A','X2'), ('A','X3'), ('B','X1'), ('B','X2'), ('B','X3'), ('C','X1'), ('C','X2'), ('C','X3') ]
mi_columns = pd.MultiIndex.from_tuples(mi_tuples, names = ['minor', 'items'])
有什么想法吗


谢谢

我认为结合使用
到_frame
取消堆叠
,以及
旋转木马
可以让你达到目的。请参见下面的一些示例数据

In [134]: pnl = pd.io.data.DataReader(['GOOG', 'AAPL'], 'yahoo')

In [135]: pnl
Out[135]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 1421 (major_axis) x 2 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2010-01-04 00:00:00 to 2015-08-25 00:00:00
Minor_axis axis: AAPL to GOOG

In [136]: df = pnl.to_frame().unstack(level=1)

In [137]: df.columns = df.columns.swaplevel(0,1)

In [138]: df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1421 entries, 2010-01-04 to 2015-08-25
Data columns (total 12 columns):
(AAPL, Open)         1421 non-null float64
(GOOG, Open)         357 non-null float64
(AAPL, High)         1421 non-null float64
(GOOG, High)         357 non-null float64
(AAPL, Low)          1421 non-null float64
(GOOG, Low)          357 non-null float64
(AAPL, Close)        1421 non-null float64
(GOOG, Close)        357 non-null float64
(AAPL, Volume)       1421 non-null float64
(GOOG, Volume)       357 non-null float64
(AAPL, Adj Close)    1421 non-null float64
(GOOG, Adj Close)    357 non-null float64
dtypes: float64(12)
memory usage: 144.3 KB
[134]中的
:pnl=pd.io.data.DataReader(['GOOG','AAPL','yahoo'))
In[135]:pnl
出[135]:
尺寸:6(项目)x 1421(长轴)x 2(短轴)
项目轴:打开到调整关闭
长轴:2010-01-04 00:00:00至2015-08-25 00:00:00
短轴:AAPL至GOOG
在[136]中:df=pnl.to_frame().取消堆栈(级别=1)
In[137]:df.columns=df.columns.swaplevel(0,1)
在[138]:df.info()中
日期时间索引:1421条,2010-01-04至2015-08-25
数据列(共12列):
(AAPL,打开)1421非空浮点64
(GOOG,Open)357非空浮点64
(AAPL,高)1421非空浮点64
(高,高)357非空浮点64
(AAPL,低)1421非空浮点64
(咕咕,低)357非空浮点64
(AAPL,关闭)1421非空浮点64
(GOOG,Close)357非空浮点64
(AAPL,卷)1421非空浮点64
(GOOG,卷)357非空浮点64
(AAPL,Adj Close)1421非空浮点64
(GOOG,Adj Close)357非空浮点64
数据类型:float64(12)
内存使用率:144.3KB

我认为将
转帧
取消堆叠
旋转木马
相结合可以实现这一目标。请参见下面的一些示例数据

In [134]: pnl = pd.io.data.DataReader(['GOOG', 'AAPL'], 'yahoo')

In [135]: pnl
Out[135]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 1421 (major_axis) x 2 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2010-01-04 00:00:00 to 2015-08-25 00:00:00
Minor_axis axis: AAPL to GOOG

In [136]: df = pnl.to_frame().unstack(level=1)

In [137]: df.columns = df.columns.swaplevel(0,1)

In [138]: df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1421 entries, 2010-01-04 to 2015-08-25
Data columns (total 12 columns):
(AAPL, Open)         1421 non-null float64
(GOOG, Open)         357 non-null float64
(AAPL, High)         1421 non-null float64
(GOOG, High)         357 non-null float64
(AAPL, Low)          1421 non-null float64
(GOOG, Low)          357 non-null float64
(AAPL, Close)        1421 non-null float64
(GOOG, Close)        357 non-null float64
(AAPL, Volume)       1421 non-null float64
(GOOG, Volume)       357 non-null float64
(AAPL, Adj Close)    1421 non-null float64
(GOOG, Adj Close)    357 non-null float64
dtypes: float64(12)
memory usage: 144.3 KB
[134]中的
:pnl=pd.io.data.DataReader(['GOOG','AAPL','yahoo'))
In[135]:pnl
出[135]:
尺寸:6(项目)x 1421(长轴)x 2(短轴)
项目轴:打开到调整关闭
长轴:2010-01-04 00:00:00至2015-08-25 00:00:00
短轴:AAPL至GOOG
在[136]中:df=pnl.to_frame().取消堆栈(级别=1)
In[137]:df.columns=df.columns.swaplevel(0,1)
在[138]:df.info()中
日期时间索引:1421条,2010-01-04至2015-08-25
数据列(共12列):
(AAPL,打开)1421非空浮点64
(GOOG,Open)357非空浮点64
(AAPL,高)1421非空浮点64
(高,高)357非空浮点64
(AAPL,低)1421非空浮点64
(咕咕,低)357非空浮点64
(AAPL,关闭)1421非空浮点64
(GOOG,Close)357非空浮点64
(AAPL,卷)1421非空浮点64
(GOOG,卷)357非空浮点64
(AAPL,Adj Close)1421非空浮点64
(GOOG,Adj Close)357非空浮点64
数据类型:float64(12)
内存使用率:144.3KB

我认为将
转帧
取消堆叠
旋转木马
相结合可以实现这一目标。请参见下面的一些示例数据

In [134]: pnl = pd.io.data.DataReader(['GOOG', 'AAPL'], 'yahoo')

In [135]: pnl
Out[135]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 1421 (major_axis) x 2 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2010-01-04 00:00:00 to 2015-08-25 00:00:00
Minor_axis axis: AAPL to GOOG

In [136]: df = pnl.to_frame().unstack(level=1)

In [137]: df.columns = df.columns.swaplevel(0,1)

In [138]: df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1421 entries, 2010-01-04 to 2015-08-25
Data columns (total 12 columns):
(AAPL, Open)         1421 non-null float64
(GOOG, Open)         357 non-null float64
(AAPL, High)         1421 non-null float64
(GOOG, High)         357 non-null float64
(AAPL, Low)          1421 non-null float64
(GOOG, Low)          357 non-null float64
(AAPL, Close)        1421 non-null float64
(GOOG, Close)        357 non-null float64
(AAPL, Volume)       1421 non-null float64
(GOOG, Volume)       357 non-null float64
(AAPL, Adj Close)    1421 non-null float64
(GOOG, Adj Close)    357 non-null float64
dtypes: float64(12)
memory usage: 144.3 KB
[134]中的
:pnl=pd.io.data.DataReader(['GOOG','AAPL','yahoo'))
In[135]:pnl
出[135]:
尺寸:6(项目)x 1421(长轴)x 2(短轴)
项目轴:打开到调整关闭
长轴:2010-01-04 00:00:00至2015-08-25 00:00:00
短轴:AAPL至GOOG
在[136]中:df=pnl.to_frame().取消堆栈(级别=1)
In[137]:df.columns=df.columns.swaplevel(0,1)
在[138]:df.info()中
日期时间索引:1421条,2010-01-04至2015-08-25
数据列(共12列):
(AAPL,打开)1421非空浮点64
(GOOG,Open)357非空浮点64
(AAPL,高)1421非空浮点64
(高,高)357非空浮点64
(AAPL,低)1421非空浮点64
(咕咕,低)357非空浮点64
(AAPL,关闭)1421非空浮点64
(GOOG,Close)357非空浮点64
(AAPL,卷)1421非空浮点64
(GOOG,卷)357非空浮点64
(AAPL,Adj Close)1421非空浮点64
(GOOG,Adj Close)357非空浮点64
数据类型:float64(12)
内存使用率:144.3KB

我认为将
转帧
取消堆叠
旋转木马
相结合可以实现这一目标。请参见下面的一些示例数据

In [134]: pnl = pd.io.data.DataReader(['GOOG', 'AAPL'], 'yahoo')

In [135]: pnl
Out[135]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 1421 (major_axis) x 2 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2010-01-04 00:00:00 to 2015-08-25 00:00:00
Minor_axis axis: AAPL to GOOG

In [136]: df = pnl.to_frame().unstack(level=1)

In [137]: df.columns = df.columns.swaplevel(0,1)

In [138]: df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1421 entries, 2010-01-04 to 2015-08-25
Data columns (total 12 columns):
(AAPL, Open)         1421 non-null float64
(GOOG, Open)         357 non-null float64
(AAPL, High)         1421 non-null float64
(GOOG, High)         357 non-null float64
(AAPL, Low)          1421 non-null float64
(GOOG, Low)          357 non-null float64
(AAPL, Close)        1421 non-null float64
(GOOG, Close)        357 non-null float64
(AAPL, Volume)       1421 non-null float64
(GOOG, Volume)       357 non-null float64
(AAPL, Adj Close)    1421 non-null float64
(GOOG, Adj Close)    357 non-null float64
dtypes: float64(12)
memory usage: 144.3 KB
[134]中的
:pnl=pd.io.data.DataReader(['GOOG','AAPL','yahoo'))
In[135]:pnl
出[135]:
尺寸:6(项目)x 1421(长轴)x 2(短轴)
项目轴:打开到调整关闭
长轴:2010-01-04 00:00:00至2015-08-25 00:00:00
短轴:AAPL至GOOG
在[136]中:df=pnl.to_frame().取消堆栈(级别=1)
In[137]:df.columns=df.columns.swaplevel(0,1)
在[138]:df.info()中
日期时间索引:1421条,2010-01-04至2015-08-25
数据列(共12列):
(AAPL,打开)1421非空浮点64
(GOOG,Open)357非空浮点64
(AAPL,高)1421非空浮点64
(高,高)357非空浮点64
(AAPL,低)1421非空浮点64
(咕咕,低)357非空浮点64
(AAPL,关闭)1421非空浮点64
(GOOG,Close)357非空浮点64
(AAPL,卷)1421非空浮点64
(GOOG,卷)357非空浮点64
(AAPL,Adj Close)1421非空浮点64
(GOOG,Adj Close)357非空浮点64
数据类型:float64(12)
内存使用率:144.3KB