如何将列表合并并转换为一个数据框?

时间:2019-11-23 16:00:26

标签: python list dataframe

import pandas as pd 
import numpy as np 
Models = ["Model1","Model2","Model3","Model4","Model5","Model6"] Subsample = [0.9,1,0.8,0.6,0.6,0.5] 
Colsample = [0.7,1,1,0.8,0.7,0.7] 
Train-rmse = [0.342072,0.337006,0.33567,0.335309,0.356839,0.358312]
Valid-rmse = [0.384494,0.382689,0.385115,0.386509,0.389526,0.39074]
Modeling-rmse = [0.38449,0.38269,0.38511,0.38651,0.38953,0.39074]
df1 = pd.DataFrame(np.column_stack([Models,Subsample,Colsamples,Train-rmse,Valid-rmse,Modeling-rmse]),columns = (["Models","Subsample","Colsample","Train-rmse","Valid-rmse","Modeling-rmse"])

无法将列表合并并将其转换为单个数据框,需要建议!

1 个答案:

答案 0 :(得分:1)

您的代码几乎一切都很好,只有您使用了一些非法的语法来命名变量。请勿在变量名中使用-

import pandas as pd 
import numpy as np 

Models = ["Model1","Model2","Model3","Model4","Model5","Model6"]
Subsample = [0.9,1,0.8,0.6,0.6,0.5] 
Colsample = [0.7,1,1,0.8,0.7,0.7]
Train_rmse = [0.342072,0.337006,0.33567,0.335309,0.356839,0.358312]
Valid_rmse = [0.384494,0.382689,0.385115,0.386509,0.389526,0.39074]
Modeling_rmse = [0.38449,0.38269,0.38511,0.38651,0.38953,0.39074]

columns = ["Models","Subsample","Colsample","Train-rmse","Valid-rmse","Modeling-rmse"]

df1 = pd.DataFrame(np.column_stack([Models,Subsample,Colsample,Train_rmse,Valid_rmse,Modeling_rmse]), columns=columns)

现在应该可以正常工作了。