2016-10-10 8 views
1

タイプのpd.DataFrame.stack()を使用してDataFrameを作成しましたが、その結果、Seriesが作成したことがわかりました。以下、DataFrame.stack()Series

enter image description here

よう

私はその結果を見た後は、)(.STACK削除され、次の結果を示しました。

enter image description here

これらの試験の違いは何ですか?

答えて

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 
関連する問題