答案 0 :(得分:1)
您需要分配输出,因为在stack
后Series
:
df = df.reset_index()
样品:
df = pd.DataFrame({'theoretic':[1.5,2.8,3.1],
'simulation':[4.7,5.8,6.2]})
print (df)
simulation theoretic
0 4.7 1.5
1 5.8 2.8
2 6.2 3.1
df = df.stack()
print (type(df))
<class 'pandas.core.series.Series'>
df = df.reset_index()
print (type(df))
<class 'pandas.core.frame.DataFrame'>
print (df)
level_0 level_1 0
0 0 simulation 4.7
1 0 theoretic 1.5
2 1 simulation 5.8
3 1 theoretic 2.8
4 2 simulation 6.2
5 2 theoretic 3.1