0

I would like to ask a question for a numpy array below.

I have a dataset, which has 50 rows and 15 columns and I created a numpy array as such:

I want to compare rows with each other (except than itself), then found the number of rows which satisfies following condition:

there is no other row that

-values are both smaller

-if one is equal, the other one should be smaller

Money Weight
10 80
20 70
30 90
25 50
35 10
40 60
50 10

for instance for row 1: there is no other row which are both smaller on two columns, if one is smaller on the other column row 1 is smaller on the other. Satisfies the condition

for row 3: there is no other row which are both smaller on two columns, it is equal on column weight with row 6 but on money dimension it is smaller. Satisfies the condition

for row 6: there is no other row which are both smaller on two columns. it is equal on weight dimension with row 3 but the value in money is greater. Does not satisfy the condition

Following code works perfect for me to find the rows in the given table:

mask = (arr <= arr[:, None]).all(2).sum(1) < 2
res = df[mask]
print(res)

But I need also compare the table with another row outside the table and should find True or False

For instance:

compare([40,10],table) =False
compare([10,70],table) =True

I have tried bunch of ways to find a proper solution, but could not find a proper way.

I appreciate any suggestions!

  • The indices in your example don't make sense. You said row 3 and row 6 have the same value in column `weight`. There are only 2 rows that match on `weight`: `35 10` and `50 10`, which has only one row between them, so they can't be row 3 and row 6 that have 2 rows in between. Please clarify your row indices by e.g., assigning an explicit index column. – kmkurn Dec 18 '22 at 09:43

0 Answers0