2

I have an array of numbers, which I would like to replace with a string depending on some condition. I can replace them with numbers:

d_sex = rand(10)
d_sex[d_sex > 0.5] = 1
d_sex[d_sex <= 0.5] = 0
d_sex

But I cannot do d_sex[d_sex>0.5] = "F". How would I do that in the most pythonic way? Would I create an empty string array? I am looking for something similar to Julia's:

d_sex = rand(50)
s_sex = ifelse(d_sex .> 0.5, "M", "F")
[d_sex s_sex]
4
  • 2
    Are you talking about numpy arrays? Commented Dec 28, 2014 at 8:40
  • I think map would be appropriate docs.python.org/2/library/functions.html#map Commented Dec 28, 2014 at 8:43
  • If you're trying to generate random zeros and ones you can use: d_sex = int(random.random() + 0.5). If that's not what you're looking for try adding more details to your question Commented Dec 28, 2014 at 8:44
  • 1
    If you want to mix data types, consider a pandas DataFrame. You can have a numpy string (or object) array, but it's not very efficient. Commented Dec 28, 2014 at 8:45

4 Answers 4

5

numpy.where is the equivalent of Julia's ifelse:

>>> np.where(d_sex > 0.5, 'M', 'F')
array(['F', 'M', 'M', 'F', 'F', 'M', 'F', 'M', 'F', 'F'], 
  dtype='|S1')
Sign up to request clarification or add additional context in comments.

Comments

2

If you don't use numpy then map is probably the best thing to do

a = [random() for _ in range(10)]
map(lambda x: 'F' if x > .5 else 'M', a)

Comments

1

Set dtype as object and it will allow you to replace float with a string as:

>>> import numpy as np
>>> d_sex = np.random.rand(10).astype('object')
>>> d_sex
array([0.6481844853562397, 0.1369951687351887, 0.4672729496950908,
       0.10511352546752228, 0.3967990781535288, 0.3452032518851482,
       0.34527110292775176, 0.24858258988637605, 0.2890001412667411,
       0.9504476492941463], dtype=object)
>>> d_sex[d_sex>0.5] = 'F'
>>> d_sex[d_sex<=0.5] = 'M'
>>> d_sex
array(['F', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'F'], dtype=object)

Comments

0

You could use a list comperhension

a = [0, 1, 0]
b = ['F' if i > 0.5 else 'M' for i in a]

=> ['M', 'F', 'M']

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.