2017-08-11 35 views
1

代表在一個特定的形式熊貓據幀數據我有兩隻大熊貓據幀作爲輸出:如何使用連擊和數據透視表

Modeling Methods(Overall Themes & FR) RMSE (CV=10) 
0         Lasso  -0.559883 
1         SVR  -0.642521 
2         NuSVR  -0.602523 
3    GradientBoostingRegressor  -0.773394 
4     RandomForestRegressor  -0.866475 

 Modeling Methods(4 Themes & FR) RMSE (CV=10) 
    0       Lasso  -0.559883 
    1        SVR  -0.655144 
    2       NuSVR  -0.639760 
    3  GradientBoostingRegressor  -0.860851 
    4   RandomForestRegressor  -0.818647 

我想加入這兩個在以下形式的數據幀:

          Lasso SVR NuSVR  GradientBoostingRegressor RandomForestRegressor 
0 Modeling Methods(4 Themes & FR)  -0.55 -0.65 -0.63   -0.86      -0.81 
1 Modeling Methods(Overall Themes & FR) -0.55 -0.64 -0.60   -0.77      -0.86 

我已經使用下面的代碼,但結果是不按照電子xpectation

frames = [factor_flood_response, only_flood_response, Theme4_flood_response,Overall_Theme_flood_response] 
result = pd.concat(frames, axis=0, join='outer') 
print(result) 

回答

0

讓我們試試這個:

pd.concat([i.set_index(i.columns[0]).rename(columns={'RMSE (CV=10)':i.columns[0]}).T for i in [df1,df2]]) 

輸出:

          Lasso  SVR  NuSVR \ 
Modeling Methos(Overall Themes & FR) -0.559883 -0.642521 -0.602523 
Modeling Methods(4 Themes & FR)  -0.559883 -0.655144 -0.639760 

             GradientBoostingRegressor \ 
Modeling Methos(Overall Themes & FR)     -0.773394 
Modeling Methods(4 Themes & FR)      -0.860851 

             RandomForestRegressor 
Modeling Methos(Overall Themes & FR)    -0.866475 
Modeling Methods(4 Themes & FR)     -0.818647 

使用列表理解和set_index,用重命名一些幾個欄目,我們可以得到上面的結果,其中:

print(df1) 

    Modeling Methos(Overall Themes & FR) RMSE (CV=10) 
0        Lasso  -0.559883 
1         SVR  -0.642521 
2        NuSVR  -0.602523 
3   GradientBoostingRegressor  -0.773394 
4    RandomForestRegressor  -0.866475 

print(df2) 

    Modeling Methods(4 Themes & FR) RMSE (CV=10) 
0       Lasso  -0.559883 
1        SVR  -0.655144 
2       NuSVR  -0.639760 
3  GradientBoostingRegressor  -0.860851 
4   RandomForestRegressor  -0.818647 
+1

感謝波士頓...這是爆炸性的 上!我知道了。通過列表理解,設置索引,數據轉置和重命名列,您的工作非常精彩。再次感謝! –