简体   繁体   中英

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

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM