0

This is my first attempt to use Python. I would appreciate any advice on how to post process data using Python.

I have a 2D array with two columns that consists of numbers: a and c. In addition, I have 1D array b that consists of some specific (and exact) values of a. What I want to do is to find c values at which a == b. My approach is to find indexes of a where a == b and then use b[a_indexes]. I fail at finding indexes.

    'a'    'c'  
     1     20   
     40    70
     83    67
     1054  90

     'b'
      40
      1054

Desired output:

40 70
1054 90

I tried:

a_indexes = a.index(b) 

But it does not work.

I have this error:

'numpy.ndarray' object has no attribute 'index'

Masoud
  • 1,270
  • 7
  • 12
Mike J
  • 1
  • 1

1 Answers1

0

I think that you want to do something like

import numpy as np

arr = np.array([[1, 20],  [40, 70], [83, 67], [1054,90]])
b = np.array([40, 1054])

output = []
for value in b:
    a_indexes = np.where(arr == value)
    for a_index in a_indexes[0]: # we look where the value was found along first dimension
        output.append(arr[a_index])
# output should be [array([40, 70]), array([1054, 90])]
print(output)

For more information on indexing check Is there a NumPy function to return the first index of something in an array? or https://thispointer.com/find-the-index-of-a-value-in-numpy-array/

Note that if you have column names, probably you are not dealing with pure numpy arrays, but maybe pandas dataframe or something else.

halucka
  • 26
  • 3