Python Forum
Replace elements of array with elements from another array based on a third array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace elements of array with elements from another array based on a third array
#1
I hope I can explain my current problem in a understandable way (besides from the possible confusing title Rolleyes):

I need to replace some elements of Array 1 with corresponding elements from Array 2, depending on what Array 3 indicates by its input (because it is based on a dropdown selection in excel) but I fail to find a proper formulation of the function that would do this.

Example:

Target Array / Array 1:

array([[0.05, 0.07 , 0.04, 0.03, 0.05,
        0.72],
       [0.08, 0.083, 0.40 , 0.038, 0.05,
        0.70],
       [0.40, 0.17, 0.07, 0.04, 0.03,
        0.31],
       [0.16, 0.14, 0.08, 0.40, 0.08,
        0.40]])
Array for replacements / Array 2:

array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])
Input Array / Array 3:

array(['No change', 'Change second element to 1',
       'Change first element to 1', 'Change fourth element to 1'],
      dtype='<U27')
Desired Output:

array([[0.05, 0.07 , 0.04, 0.03, 0.05,
        0.72],
       [0, 1, 0, 0],
       [1, 0, 0, 0],
      [0, 0, 0, 1]])
So basically everytime the input Array 3 says "Change x element", the corresponding element of Array 1 should be replaced with the row of Array 2 so that one value becomes 1 an all others 0.

I already tried several ways to interpret the Input Array and then iterate over the target array, but there was no succesful attempts so far. Maybe you got an idea. Huh

Thanks in advance!
Reply


Messages In This Thread
Replace elements of array with elements from another array based on a third array - by Cola_Reb - May-13-2022, 01:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python code to calculate mean of an array of numbers using numpy viren 3 175 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 223 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Good way to ignore case when searching elements? Winfried 1 334 Apr-25-2024, 12:39 PM
Last Post: menator01
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 586 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 351 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 6,102 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  unable to remove all elements from list based on a condition sg_python 3 583 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How Write Part of a Binary Array? Assembler 1 443 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 656 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,569 Nov-28-2023, 08:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020