python - Numpy: Fix array with rows of different lengths by filling the empty elements with zeros -
the functionality looking looks this:
data = np.array([[1, 2, 3, 4], [2, 3, 1], [5, 5, 5, 5], [1, 1]]) result = fix(data) print result [[ 1. 2. 3. 4.] [ 2. 3. 1. 0.] [ 5. 5. 5. 5.] [ 1. 1. 0. 0.]]
these data arrays i'm working large appreciate efficient solution.
edit: data read in disk python list of lists.
this 1 approach -
def numpy_fillna(data): # lengths of each row of data lens = np.array([len(i) in data]) # mask of valid places in each row mask = np.arange(lens.max()) < lens[:,none] # setup output array , put elements data 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)
Comments
Post a Comment