2

Scenario: I am testing a code I found at http://www.pythonforfinance.net/2017/01/21/investment-portfolio-optimisation-with-python/ to create some portfolios based on my data.

Issue: In the example given, the code is run with 4 stocks, while I am running my code with 779. there is a line in the code that converts one array into a pandas dataframe:

results_frame = pd.DataFrame(results.T,columns=['ret','stdev','sharpe',stocks[0],stocks[1],stocks[2],stocks[3]])

Is this line, columns are created for 4 stocks.

Question: Since i am doing this with 779, it would take to long to input the entry for each stock (stocks[0] to stocks[779]). Is there a non-manual way to do this? maybe with a loop?

1
  • results.T.columns=['ret','stdev','sharpe'] + stocks Commented Sep 24, 2017 at 16:51

1 Answer 1

1

If in stocks are columns names and it is list you need:

columns=['ret','stdev','sharpe'] + stocks
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir, you just saved me from doing that by hand in excel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.