我正在尝试取消堆叠数据帧对其执行操作(仅随着时间的推移),然后将其堆叠在一起,如下所示:
import pandas as pd
import numpy as np
from itertools import *
time = pd.date_range(pd.datetime(2007,1,1),pd.datetime(2007,1,2))
slot = map(lambda n:"s-"+str(n),reversed(range(2)))
obj = map(lambda n:"o-"+str(n),reversed(range(2)))
idx = pd.MultiIndex.from_tuples(list(product(slot, obj, time)), names=['Ananas','Bananas','time']) #list(.) needed to get a length, should this really be needed?
data = np.random.rand(len(idx),4)
df = pd.DataFrame(data=data,index=idx, columns=['a','b','c','d']) #why is idx.size==0?
print df.to_string()
print "====="
unstacked = df.unstack(level=[0,1])
print unstacked.to_string()
print "====="
stacked = unstacked.stack(level=[2,1])
print stacked.to_string()
问题是多索引在操作后被反转了,有什么简单的方法可以让它工作吗?也许我从一开始就滥用堆栈?