Problem: I have a table where I need to extract all valid combinations of rows (or columns if I transpose the table). the columns only have values "+" or "-" and a combination is considered valid when in at least one of the lines of the combination has "+", that is, any combination with "-" in all the lines is not valid.
Example table:
Guns P_01 P_02 P_03 P_04 P_05 P_06 P_07
0 G_01 + - + + + - +
1 G_02 + + + - + + -
2 G_03 - - - + + + +
3 G_04 + + + - - - -
4 G_05 + + + - - - -
5 G_06 - - - + + + +
6 G_07 + + + - - - -
Example valid combination:
0 G_01 + - + + + - +
1 G_02 + + + - + + -
Example invalid combination:
3 G_04 + + + - - - -
4 G_05 + + + - - - -
To get all the combinations, I try to use the itertools combination, and put the result into a list:
dfcomb = []
dfcomb = df.apply(lambda r: list(combinations(r, 2)), axis=0)
Output:
Guns P_01 P_02 P_03 P_04 P_05 P_06 P_07
0 (G_01, G_02) (+, +) (-, +) (+, +) (+, -) (+, +) (-, +) (+, -)
1 (G_01, G_03) (+, -) (-, -) (+, -) (+, +) (+, +) (-, +) (+, +)
2 (G_01, G_04) (+, +) (-, +) (+, +) (+, -) (+, -) (-, -) (+, -)
3 (G_01, G_05) (+, +) (-, +) (+, +) (+, -) (+, -) (-, -) (+, -)
4 (G_01, G_06) (+, -) (-, -) (+, -) (+, +) (+, +) (-, +) (+, +)
5 (G_01, G_07) (+, +) (-, +) (+, +) (+, -) (+, -) (-, -) (+, -)
6 (G_02, G_03) (+, -) (+, -) (+, -) (-, +) (+, +) (+, +) (-, +)
7 (G_02, G_04) (+, +) (+, +) (+, +) (-, -) (+, -) (+, -) (-, -)
8 (G_02, G_05) (+, +) (+, +) (+, +) (-, -) (+, -) (+, -) (-, -)
9 (G_02, G_06) (+, -) (+, -) (+, -) (-, +) (+, +) (+, +) (-, +)
10 (G_02, G_07) (+, +) (+, +) (+, +) (-, -) (+, -) (+, -) (-, -)
11 (G_03, G_04) (-, +) (-, +) (-, +) (+, -) (+, -) (+, -) (+, -)
12 (G_03, G_05) (-, +) (-, +) (-, +) (+, -) (+, -) (+, -) (+, -)
13 (G_03, G_06) (-, -) (-, -) (-, -) (+, +) (+, +) (+, +) (+, +)
14 (G_03, G_07) (-, +) (-, +) (-, +) (+, -) (+, -) (+, -) (+, -)
15 (G_04, G_05) (+, +) (+, +) (+, +) (-, -) (-, -) (-, -) (-, -)
16 (G_04, G_06) (+, -) (+, -) (+, -) (-, +) (-, +) (-, +) (-, +)
17 (G_04, G_07) (+, +) (+, +) (+, +) (-, -) (-, -) (-, -) (-, -)
18 (G_05, G_06) (+, -) (+, -) (+, -) (-, +) (-, +) (-, +) (-, +)
19 (G_05, G_07) (+, +) (+, +) (+, +) (-, -) (-, -) (-, -) (-, -)
20 (G_06, G_07) (-, +) (-, +) (-, +) (+, -) (+, -) (+, -) (+, -)
But now I'm stuck, I know that I should use a loop to verify if any combination is valid or not, but how can I do that?