![](/img/trans.png)
[英]check if values of a column are in values of another numpy array column in pandas
[英]check if values of a column are in values of another (numpy array)
我有一個 numpy 數組,我想創建一個新的數組,僅當對於每一行,columnX 中的元素在 columnY 的每一行中都不存在時才附加該行。
我想過一個 for 循環來做到這一點,但它不起作用。
array = [()]
for row in data:
if data[:, 0] == data[:, 6]:
np.append(array, row)
我知道你說 numpy 但在 pandas 中這很容易
df = pd.DataFrame(data, columns=[x,y])
df = df[~df.x.isin(df.y)]
output = df.values # back to numpy
從@Jordan 的回答中猜,結果數組應該是這樣的:
array = np.array([[1, 2], [2, 3], [3, 4], [5, 6], [4, 3]])
array[~np.isin(array[:, 0], array[:, 1])]
並得到
array([[1, 2],
[5, 6]])
我不確定我是否明白你的問題的重點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.