嗨,我需要使用不同名称的数据框。数据框正在创建insde for循环,如下所示。
for lambda_ in range(0,len(tuned_parameter)-1):
print ('....................................',lambda_)
i=0
for train_index, test_index in kf.split(X):
print("Train:", train_index, "Validation:",test_index)
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
print ('X train ............',X_train.shape)
print ('X_test...............',X_test.shape)
mean_returns_Train = X_train.mean()
cov_matrix_Train=X_train.cov()
mean_returns_Test = X_test.mean()
cov_matrix_Test = X_test.cov()
max_s, min_s = display_simulated_ef_with_random(mean_returns_Train, cov_matrix_Train, num_portfolios, risk_free_rate,returns, tuned_parameter[lambda_])
#print (max_s.T)
appended_data.append(max_s.T)
appended_data = pd.concat(appended_data, axis=1)
print (appended_data.mean(axis=1))
i=i+1
但是我需要我的for循环像,appended_data0,appended_data1,appended_data3 .....
如何创建多个数据框,其名称以相关的“ i”值结尾
答案 0 :(得分:0)
该问题通过如下调整进行了处理。如评论部分所建议,使用字典来存储数据框。
whole_dataframes = {}
#k = int(np.floor(float(X.shape[0]) / number_folds))
weights = np.zeros((3,num_portfolios))
for lambda_ in range(0,len(tuned_parameter)-1):
print ('....................................',lambda_)
i=0
appended_data = []
for train_index, test_index in kf.split(X):
print("Train:", train_index, "Validation:",test_index)
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
print ('X train ............',X_train.shape)
print ('X_test...............',X_test.shape)
mean_returns_Train = X_train.mean()
cov_matrix_Train=X_train.cov()
mean_returns_Test = X_test.mean()
cov_matrix_Test = X_test.cov()
max_s, min_s = display_simulated_ef_with_random(mean_returns_Train, cov_matrix_Train, num_portfolios, risk_free_rate,returns, tuned_parameter[lambda_])
#print (max_s.T)
appended_data.append(max_s.T)
appended_data = pd.concat(appended_data, axis=1)
whole_dataframes['Variable{}'.format(i)]=appended_data.mean(axis=1)
print (whole_dataframes['Variable{}'.format(i)])
i=i+1