DGMS89
DGMS89

Reputation: 1677

Adding multiple column names when creating a pandas dataframe

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?

Upvotes: 2

Views: 484

Answers (1)

jezrael
jezrael

Reputation: 863651

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

columns=['ret','stdev','sharpe'] + stocks

Upvotes: 1

Related Questions