Skip to content Skip to sidebar Skip to footer

How To Repeat A Series Of Numbers With Shape (n,1) As An Array Of N Unique Numbers Repeated By 4 Times With A Shape Of (n,1)

I think this is a relatively straightfoward question but I've been struggling with getting this to the right shape. I have a series/dataframe column structured as: 0 0.1278

Solution 1:

How about this method:

import numpy as np
ser = np.arange(5, dtype = float) # Edit: added argument 'dtype = float'
arr = ser.repeat(4).reshape(-1, 4)
l = list(arr)
ob_arr = np.array([None for i in range(len(l))], dtype = object)
for i in range(len(ob_arr)):
    ob_arr[i] = l[i]

output:

array([array([0., 0., 0., 0.]), array([1., 1., 1., 1.]),
       array([2., 2., 2., 2.]), array([3., 3., 3., 3.]),
       array([4., 4., 4., 4.])], dtype=object)

Post a Comment for "How To Repeat A Series Of Numbers With Shape (n,1) As An Array Of N Unique Numbers Repeated By 4 Times With A Shape Of (n,1)"