Home > Mobile >  check if values of a column are in values of another (numpy array)
check if values of a column are in values of another (numpy array)

Time:08-29

I have a numpy array and I want to create a new one appending the row only if for each row, the element in the columnX is absent in every row of the columnY.

I thought about a for loop to do it, but it doesn't work.

array = [()]
for row in data:
    if data[:, 0] == data[:, 6]:
         np.append(array, row)

CodePudding user response:

I know you said numpy but it is this easy in pandas

df = pd.DataFrame(data, columns=[x,y])
df = df[~df.x.isin(df.y)]
output = df.values # back to numpy

CodePudding user response:

Guess from @Jordan's answer, the resulting array should be like:

array = np.array([[1, 2], [2, 3], [3, 4], [5, 6], [4, 3]])
array[~np.isin(array[:, 0], array[:, 1])]

and get

array([[1, 2],
       [5, 6]])

I'm not sure if I get the point of your question.

  • Related