Replacing empty or missing values with zeros in a large array

3494 views python

I have an large array of more than 40000 elements

a = ['15', '12', '', 18909, ...., '8989', '', '90789', '8']

I'm looking for a simply way to replace the empty '' values to '0' so that I can manipulate the data in the array using Numpy.

I would then convert the elements in my array into integers using

a = map(int, a)

so that I could find the mean of the array in numpy

a_mean = np.mean(a)

My issue is that I cannot convert to integers in an array with missing numbers to get a mean.

answered question

Can you do: new_a = [int(v or 0) for v in a] and then use new_a?

I believe you can use numpy.nan_to_num ?

4 Answers


If I understood you right so it should look like that:

for index in range(len(a)):
    if a[i] is '':
       a[i] = '0'

You can also use:

a = list(map(lambda x: '0' if x == '' else x, a))

posted this

A more verbose answer is:

acc = 0
for v in a:
    acc+=int(v or 0)
a_mean = acc/len(a)

posted this

From the previous learning with SO, i see you can impy the below solution to convert the NaN to zeros..

from numpy import *

a = array([[0, 1, 2], [3, 4, NaN]])
where_are_NaNs = isnan(a)
a[where_are_NaNs] = 0

secondly, nan_to_num() as i said earlier in my comment.

posted this

You could just make a small function that does exactly what you want, e.g.:

def to_int(x):
        return int(x)
    except ValueError:
        return 0

And then use that with map(to_int, a).

In [22]: a = ['15', '12', '', 18909, '8989', '90789', '8']

map(to_int, a)
Out[23]: [15, 12, 0, 18909, 8989, 90789, 8]

posted this

Have an answer?


Please login first before posting an answer.