Add third dimension to a 2-dimensional numpy.ndarray

Question:

I have an array which contains 50 time series. Each time series has 50 values.
The shape of my array is therefore:

print(arr.shape) = (50,50)

I want to extract the 50 time series and I want to assign a year to each of them:

years = list(range(1900,1950))
print(len(years)) = 50

The order should be maintained. years[0] should correspond with arr[0,:] (this is the first time series).

I am glad for any help!

Edit: This is the small example

import random

years = list(range(1900,1904))
values = random.sample(range(10, 30), 16) 
arr = np.reshape(values, (4, 4))
Asked By: brix

||

Answers:

Let’s say you have the following data:

import numpy as np

data = np.random.randint(low=1, high=9, size=(5, 4))
years = np.arange(1900, 1905)

You can use np.concatenate:

>>> arr = np.concatenate([years[:, None], data], axis=1)
>>> arr

array([[1900,    5,    8,    1,    2],
       [1901,    3,    3,    1,    5],
       [1902,    7,    4,    7,    5],
       [1903,    1,    6,    6,    4],
       [1904,    4,    5,    3,    8]])

or maybe use a pandas.DataFrame:

>>> import pandas as pd

>>> df = pd.DataFrame(data)
>>> df = df.assign(year=years)
>>> df = df.set_index("year")
>>> df

      0  1  2  3
year
1900  3  2  8  1
1901  5  8  5  2
1902  3  5  4  3
1903  6  2  7  6
1904  8  8  4  6
Answered By: paime