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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With