3

I have two two-dimensional arrays, and I have to create a new array filtering through the 2nd array where 1st column indexes match. The arrays are of different size.

basically the idea is as follow:

file A

#x   y
1   2
3   4
2   2
5   4
6   4
7   4


file B

#x1    y1
0       1
1       1
11      1
5       1
7       1
My expected output 2D array should look like

#newx     newy
1         1
5         1
7         1

I tried it following way:

match =[]
for i in range(len(x)):
    if x[i] == x1[i]:
        new_array = x1[i]
        match.append(new_array)

print match

This does not seem to work. Please suggest a way to create the new 2D array

6 Answers 6

5

Try np.isin.

arr1 = np.array([[1,3,2,5,6,7], [2,4,2,4,4,4]])
arr2 = np.array([[0,1,11,5,7], [1,1,1,1,1]])
arr2[:,np.isin(arr2[0], arr1[0])]
array([[1, 5, 7],
       [1, 1, 1]])

np.isin(arr2[0], arr1[0]) checks whether each element of arr2[0] is in arr1[0]. Then, we use the result as the boolean index array to select elements in arr2.

Sign up to request clarification or add additional context in comments.

Comments

5

If you make a set out of the first element in A, then it is fairly easy to find the elements in B to keep like:

Code:

a = ((1, 2), (3, 4), (2, 2), (5, 4), (6, 4), (7, 4))
b = ((0, 1), (1, 1), (11, 1), (5, 1), (7, 1))

in_a = {i[0] for i in a}
new_b = [i for i in b if i[0] in in_a]

print(new_b)

Results:

[(1, 1), (5, 1), (7, 1)]

Output results to file as:

with open('output.txt', 'w') as f:
    for value in new_b:
        f.write(' '.join(str(v) for v in value) + '\n')

3 Comments

could you please explain how I make pairs from my large arrays? like the way you created the a and b array?
@bhjghjh, I would love to, if I understood what you were asking. Maybe you could formulate another question? If you do ask another question, feel free to leave a comment here if you want to be sure I see it. Good Luck...
thanks a lot for helps. In my original problem, I do not have (x,y) as pairs. I just have two different .txt files from which I am reading the two columns as arrays. Could you please modify your code to output the result as two different columns in a file?
2
#!/usr/bin/env python3

from io import StringIO
import pandas as pd

fileA = """x   y
1   2
3   4
2   2
5   4
6   4
7   4
"""

fileB = """x1    y1
0       1
1       1
11      1
5       1
7       1
"""


df1 = pd.read_csv(StringIO(fileA), delim_whitespace=True, index_col="x")
df2 = pd.read_csv(StringIO(fileB), delim_whitespace=True, index_col="x1")

df = pd.merge(df1, df2, left_index=True, right_index=True)
print(df["y1"])

# 1    1
# 5    1
# 7    1

https://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging

Comments

2

If you use pandas:

import pandas as pd

A = pd.DataFrame({'x': pd.Series([1,3,2,5,6,7]), 'y': pd.Series([2,4,2,4,4,4])})
B = pd.DataFrame({'x1': pd.Series([0,1,11,5,7]), 'y1': 1})

C = A.join(B.set_index('x1'), on='x')

Then if you wanted to drop the unneeded row/columns and rename the columns:

C = A.join(B.set_index('x1'), on='x')
C = C.drop(['y'], axis=1)
C.columns = ['newx', 'newy']

which gives you:

>>> C
   newx  newy
0     1   1.0
3     5   1.0
5     7   1.0

If you are going to work with arrays, dataframes, etc - pandas is definitely worth a look: https://pandas.pydata.org/pandas-docs/stable/10min.html

Comments

1

Assuming that you have (x, y) pairs in your 2-D arrays, a simple loop may work:

arr1   = [[1, 2], [3, 4], [2, 2]]
arr2   = [[0, 1], [1, 1], [11, 1]]
result = []

for pair1 in arr1:
    for pair2 in arr2:
        if (pair1[0] == pair2[0]):
            result.append(pair2)

print(result)

Comments

1

Not the best solution for smaller arrays, but for really large arrays, works fast -

import numpy as np
import pandas as pd

n1 = np.transpose(np.array([[1,3,2,5,6,7], [2,4,2,4,4,4]]))
n2 = np.transpose(np.array([[0,1,11,5, 7], [1,1,1,1,1]]))
np.array(pd.DataFrame(n1).merge(pd.DataFrame(n2), on=0, how='inner').drop('1_x', axis=1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.