1

I have an array that consists of numeric values that are on a string form along with the '?' character which represents missing values. How can I make to replace those chars with an outlier value like -999999?

I know there is a way with Panda´s, but how to do that only using numpy? Tried to find those values with something like:

x=X[X=='?']

to use numpy replace, but no luck at all. Any help?

Thanks

3 Answers 3

4

It's quite similar to the Pandas method:

X[X == '?'] = '-999999'

at which point, if you desire, you could convert the values to integers using

X = X.astype(int)

For example:

import numpy as np
X = np.array(['1', '2', '3', '?', '5'], dtype=object)
X[X == '?'] = '-999999'
X = X.astype(int)
print(X)
# array([      1,       2,       3, -999999,       5])
Sign up to request clarification or add additional context in comments.

Comments

2

One way to do it would be using numpy.place :

np.place(X,X=="?",-999999)

Comments

0

You can also use np.where:

print(np.where(X == '?', '-999999', X).astype(int))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.