0

I am writing a program to show the age of 100 people whose ages range between 18 and 110. I want to set a negative value for a couple of them and an age bigger than 120 for other 3 people, then clean the dataset setting 0 in these 5 "wrong" ages, and then check the minimum and the maximum of the ages after the cleaning.

I have a solution by setting the wrong ages individually, but I would like to be able to do this by creating one array with 95 elements (the correct ages), another with 5 elements (the wrong ages), and then concatenate them. However I don´t know how to concatenate 1-D arrays that were generated differently.

Here is my existing solution:

peopleAge = np.random.randint(18,110,100)
peopleAge[12] = -6
peopleAge[15] = -23
peopleAge[29] = 132
peopleAge[80] = 155
peopleAge[99] = 200
peopleAge = np.where((peopleAge < 0) | (peopleAge > 120), 0, peopleAge)

minAge = min(peopleAge[peopleAge > 0])
maxAge = max(peopleAge[peopleAge < 111])

And here is what I am trying to do:

peopleAge = np.random.randint(18,110,95)
peopleAgeSpecial = np.array([-6,-23,132,155,200])

How can I join peopleAge and peopleAgeSpecial into one array?

3
  • What do you want the result to look like? One option produces a (100,) shape array. Commented Feb 15, 2019 at 17:31
  • The result will look the same as shown in the existing solution, but the way to build the 100-item array will be different. I would like to know how I can arrive at the same result by concatenating two arrays. Commented Feb 15, 2019 at 17:33
  • 1
    You could either use a binary vector that selects the elements where you want to change you array or use the positions of the array directly wrong_binary = np.array([0, 0, 0, 1, 1, 0, 0, ... ,1, 0]) # something like that peopleAge = np.random.randint(18,110,95) peopleAge[wrong_binary] = np.array([-6,-23,132,155,200]) or wrong = np.array([12, 15, 29, 80, 99]) peopleAge = np.random.randint(18,110,95) peopleAge[wrong] = np.array([-6,-23,132,155,200]) Commented Feb 15, 2019 at 20:13

1 Answer 1

1

If you don't want to set the exact same index but just have five strange ages randomly distributed:

peopleAge = np.random.randint(18,110,95)
peopleAgeSpecial = np.array([-6,-23,132,155,200])

peopleAge = np.append(peopleAge, peopleAgeSpecial, axis = 0)
np.random.shuffle(peopleAge)
Sign up to request clarification or add additional context in comments.

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.