How do I put each stock’s returns in its own column?

Turn one array of returns per stock into a matrix with one row per day and one column per stock, check which way round it is, and read one stock or one day from it.

After this lesson, you can turn separate stock returns into one matrix and read a stock or a day from it.

How do I put each stock in a column?

np.column_stack([aaa, bbb, ccc]) places the three arrays side by side, so the matrix has one row per day and one column per stock, here eight rows and three columns. Position 0 of each array becomes row 0, so all three must cover the same days in the same order.

The returns are three made-up stocks’ daily returns in decimals from 2 to 11 March 2026. In returns[row, column], positions start at 0 and : takes all of them, so returns[:, 0] is AAA’s column and returns[0, 1] is BBB on 2 March. The tickers and dates stay outside the numbers, in their own lists.

The following box builds the matrix and reads a column and a cell.

import numpy as np

tickers = ["AAA", "BBB", "CCC"]
aaa = np.array([0.0120, -0.0085, 0.0034, -0.0151, 0.0062, 0.0097, -0.0023, 0.0045])
bbb = np.array([-0.0064, 0.0102, -0.0130, 0.0058, -0.0011, 0.0076, 0.0149, -0.0092])
ccc = np.array([0.0031, -0.0047, 0.0088, 0.0015, -0.0203, 0.0124, 0.0006, 0.0071])
returns = np.column_stack([aaa, bbb, ccc])
print(returns.shape)
print(np.array_equal(returns[:, 0], aaa), returns[0, 1])
(8, 3)
True -0.0064

np.vstack([aaa, bbb, ccc]) puts the stocks in rows instead, and .T swaps rows and columns, so np.vstack(...).T gives the same matrix. A missing return keeps its day as np.nan.

How do I read one day across the stocks?

returns[4] selects row 4, the three returns of 6 March, and dict(zip(tickers, returns[4].tolist())) pairs each ticker with its return. On 6 March, CCC lost 2.03%.

.tolist() turns NumPy’s numbers into plain Python numbers, which print without their type.

The following box prints 6 March by ticker.

print(dict(zip(tickers, returns[4].tolist())))
{'AAA': 0.0062, 'BBB': -0.0011, 'CCC': -0.0203}

The lessons on one result per stock and portfolio weights use this layout, one day per row.

Your turn

Add DDD’s returns as a fourth column and print its returns on the first and last day. You should see 0.0052 and 0.0037.

np.column_stack adds a column on the right.

ddd = np.array([0.0052, -0.0018, 0.0071, -0.0094, 0.0023, 0.0040, -0.0061, 0.0037])
four_stocks = np.column_stack([returns, ddd])
print(four_stocks[0, 3], four_stocks[-1, 3])
0.0052 0.0037