Numpy: Fix array with rows of different lengths by filling the empty elements with zeros
Solution 1:
This could be one approach -
def numpy_fillna(data):
# Get lengths of each row of data
lens = np.array([len(i) for i in data])
# Mask of valid places in each row
mask = np.arange(lens.max()) < lens[:,None]
# Setup output array and put elements from data into masked positions
out = np.zeros(mask.shape, dtype=data.dtype)
out[mask] = np.concatenate(data)
return out
Sample input, output -
In [222]: # Input object dtype array
...: data = np.array([[1, 2, 3, 4],
...: [2, 3, 1],
...: [5, 5, 5, 5, 8 ,9 ,5],
...: [1, 1]])
In [223]: numpy_fillna(data)
Out[223]:
array([[1, 2, 3, 4, 0, 0, 0],
[2, 3, 1, 0, 0, 0, 0],
[5, 5, 5, 5, 8, 9, 5],
[1, 1, 0, 0, 0, 0, 0]], dtype=object)
Solution 2:
You could use pandas instead of numpy:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([[1, 2, 3, 4],
...: [2, 3, 1],
...: [5, 5, 5, 5],
...: [1, 1]], dtype=float)
In [3]: df.fillna(0.0).values
Out[3]:
array([[ 1., 2., 3., 4.],
[ 2., 3., 1., 0.],
[ 5., 5., 5., 5.],
[ 1., 1., 0., 0.]])
Solution 3:
use np.pad()
.
In [62]: arr
Out[62]:
[array([0]),
array([83, 74]),
array([87, 61, 23]),
array([71, 3, 81, 77]),
array([20, 44, 20, 53, 60]),
array([54, 36, 74, 35, 49, 54]),
array([11, 36, 0, 98, 29, 87, 21]),
array([ 1, 22, 62, 51, 45, 40, 36, 86]),
array([ 7, 22, 83, 58, 43, 59, 45, 81, 92]),
array([68, 78, 70, 67, 77, 64, 58, 88, 13, 56])]
In [63]: max_len = np.max([len(a) for a in arr])
In [64]: np.asarray([np.pad(a, (0, max_len - len(a)), 'constant', constant_values=0) for a in arr])
Out[64]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[83, 74, 0, 0, 0, 0, 0, 0, 0, 0],
[87, 61, 23, 0, 0, 0, 0, 0, 0, 0],
[71, 3, 81, 77, 0, 0, 0, 0, 0, 0],
[20, 44, 20, 53, 60, 0, 0, 0, 0, 0],
[54, 36, 74, 35, 49, 54, 0, 0, 0, 0],
[11, 36, 0, 98, 29, 87, 21, 0, 0, 0],
[ 1, 22, 62, 51, 45, 40, 36, 86, 0, 0],
[ 7, 22, 83, 58, 43, 59, 45, 81, 92, 0],
[68, 78, 70, 67, 77, 64, 58, 88, 13, 56]])