Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting empty strings to 0 using Numpy

Tags:

python

numpy

I have a numpy array where each element looks something like this:

['3' '1' '35' '0' '0' '8.05' '2']
['3' '1' '' '0' '0' '8.4583' '0']
['1' '1' '54' '0' '0' '51.8625' '2']

I would like to replace all empty strings like the ones in the second row above, with some default value like 0. How can I do this with numpy?

The ultimate goal is to be able to run this: S.astype(np.float), but I suspect the empty strings are causing problems in the conversion.

like image 707
Oleksi Avatar asked Apr 10 '13 21:04

Oleksi


2 Answers

If your array is t:

t[t=='']='0'

and then convert it.

Explanation:

t=='' creates a boolean array with the same shape as t that has a True value where the corresponding t value is an empty space. This boolean array is then used to assign '0' only to the appropriate indices in the original t.

like image 165
Bitwise Avatar answered Oct 04 '22 02:10

Bitwise


Here is an approach that uses map not that is does not produce the same data type as calling .astype():

def FloatOrZero(value):
    try:
        return float(value)
    except:
        return 0.0


print map(FloatOrZero, ['3', '1', '', '0', '0', '8.4583', '0'])

Outputs:

[3.0, 1.0, 0.0, 0.0, 0.0, 8.4583, 0.0]

It's possible that this approach will give you more flexibility to cleanup data but it could also be harder to reason about if you are wanting to work with a numpy.array.

like image 44
Jason Sperske Avatar answered Oct 04 '22 02:10

Jason Sperske