numpy check two array on multiple column
Multi tool use
up vote
0
down vote
favorite
Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?
res = np.array(, dtype=np.float64).reshape(0,6)
for line in edata:
ind = line[1] == tdata[:,1]
ind = line[2] == tdata[ind,2]
if np.any(ind):
res = np.vstack((res,line))
python numpy
add a comment |
up vote
0
down vote
favorite
Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?
res = np.array(, dtype=np.float64).reshape(0,6)
for line in edata:
ind = line[1] == tdata[:,1]
ind = line[2] == tdata[ind,2]
if np.any(ind):
res = np.vstack((res,line))
python numpy
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?
res = np.array(, dtype=np.float64).reshape(0,6)
for line in edata:
ind = line[1] == tdata[:,1]
ind = line[2] == tdata[ind,2]
if np.any(ind):
res = np.vstack((res,line))
python numpy
Lets say edata and tdata two 2D numpy array with shape (x,6) and (y,6) where x and y are two arbitrary number and must not be equal. Now I have to check if there is any row in edata such that both 2nd and 3rd column value of that row equals to that of some row of tdata. If there is any such row then save them in another. For now I have written the following code. But I think numpy has better way to that instead of explicitly iterating and checking for values. Can you suggest me more efficient way?
res = np.array(, dtype=np.float64).reshape(0,6)
for line in edata:
ind = line[1] == tdata[:,1]
ind = line[2] == tdata[ind,2]
if np.any(ind):
res = np.vstack((res,line))
python numpy
python numpy
asked Nov 22 at 14:54
Eular
4731724
4731724
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can use equal
and outer
to see if any value in a column of edata
is in a column of tdata
. You check for both the 2nd and 3rd columns, then use any
on axis=1
to get edata
rows as required such as:
res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
&np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]
for example, with simple input:
edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12
print (res)
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
which are the last two rows of edata
that have the same value in column 2 and 3 than the first two rows of tdata
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use equal
and outer
to see if any value in a column of edata
is in a column of tdata
. You check for both the 2nd and 3rd columns, then use any
on axis=1
to get edata
rows as required such as:
res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
&np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]
for example, with simple input:
edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12
print (res)
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
which are the last two rows of edata
that have the same value in column 2 and 3 than the first two rows of tdata
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
add a comment |
up vote
1
down vote
You can use equal
and outer
to see if any value in a column of edata
is in a column of tdata
. You check for both the 2nd and 3rd columns, then use any
on axis=1
to get edata
rows as required such as:
res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
&np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]
for example, with simple input:
edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12
print (res)
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
which are the last two rows of edata
that have the same value in column 2 and 3 than the first two rows of tdata
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use equal
and outer
to see if any value in a column of edata
is in a column of tdata
. You check for both the 2nd and 3rd columns, then use any
on axis=1
to get edata
rows as required such as:
res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
&np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]
for example, with simple input:
edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12
print (res)
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
which are the last two rows of edata
that have the same value in column 2 and 3 than the first two rows of tdata
You can use equal
and outer
to see if any value in a column of edata
is in a column of tdata
. You check for both the 2nd and 3rd columns, then use any
on axis=1
to get edata
rows as required such as:
res = edata[ (np.equal.outer(edata[:,1],tdata[:,1])
&np.equal.outer(edata[:,2],tdata[:,2])).any(1) ,:]
for example, with simple input:
edata = np.arange(4*6).reshape(4,6)
tdata = np.arange(6*6).reshape(6,6) + 12
print (res)
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
which are the last two rows of edata
that have the same value in column 2 and 3 than the first two rows of tdata
answered Nov 22 at 15:37
Ben.T
5,2872523
5,2872523
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
add a comment |
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
check for this two [[1,2,3],[2,3,4],[4,5,6]] and [[1,2,3],[1,3,4],[7,5,6]], it should return the 1st row only
– Eular
Nov 22 at 19:00
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
@Eular your method (like mine) return all the rows, and it makes sense because 2nd and 3rd column in both arrays are the same, no? if it is supposed to return only the 1st row then matching both 2nd and 3rd columns is not enough, but your question (and the code associated) seems to state only matching 2nd and 3rd columns
– Ben.T
Nov 22 at 19:23
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
Sry, i was checking with 1st and 2nd column, my bad
– Eular
Nov 22 at 19:44
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
@Eular no pb :)
– Ben.T
Nov 22 at 19:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433563%2fnumpy-check-two-array-on-multiple-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
RZoziVrc0ov0Ig Anr,7gO P0Rh2iXw5a,dy