I want to create a dataframe that takes stock1, stock2 and optimal as the index;
while the mean, std, sharpe, weight, wealth are the columns. I have already generated the respective column values. I need help in generating the expected output.
Input:
stock1 = pd.Series({
'mean': mean1,
'std': std1,
'sharpe': mean1 / std1,
'weight': w1,
'wealth': stk1_wealth
})
stock2 = pd.Series({
'mean': mean2,
'std': std2,
'sharpe': mean2 / std2,
'weight': w2,
'wealth': stk2_wealth
})
optimal = pd.Series({
'mean': returns,
'std': volatility,
'sharpe': sharpe,
'weight': w1+w2,
'wealth': opt_wealth
})
stats_df = stk1_stats_df.append(stk2_stats_df).append(optimal_stats_df)
Actual output:
mean 0.00155525
std 0.0113922
sharpe 0.136519
weight 0.479314
wealth [[14555.0]]
mean 0.00188339
std 0.0124908
sharpe 0.150783
weight 0.520686
wealth [[15756.0]]
mean 0.00172611
std 0.00957114
sharpe 0.180345
weight 1
wealth 15180
dtype: object
Expected output:
mean std sharpe weight wealth
stock1 0.0016 0.0113 0.1365 0.48 14555.0
stock2 0.0018 0.0124 0.1507 0.52 15756.0
optimal 0.0017 0.0096 0.1803 1.0 15180.0
ValueError: Length of passed values is 3, index implies 5.pd.Seriesas a row in apd.DataFrame, and that is what the question is about.ValueErrorcould be from some other part of your code. Conceptually, the recipe proposed in the first answer should solve your problem.