Logical Operation on two different dataframes

Viewed 58

I have 3 dataframes of the sizes 1x100 , 20x100 and 20x100.

a1    b1 b2 .... b20    c1 c2 .... c20
0      1  0 ....  0      1  0 ....  0
1      0  1 ....  1      0  0 ....  0
0      0  0 ....  0      1  1 ....  1
1      0  0 ....  0      0  0 ....  1
0      1  1 ....  1      1  0 ....  0
1      0  0 ....  1      1  0 ....  0

I want to run a logical operation as ((a1==0) & (b1==1) & (c1==1)).astype(int) then ((a1==0) & (b2==1) & (c2==1)).astype(int) and so on until ((a1==0) & (b20==1) & (c20==1)).astype(int) and store in a new dataframe.

The final output dataframe should consist of 20 columns.

3 Answers

Because there are different columns names is necessary convert DataFrames to numpy arrays:

df = (df2.eq(1) & df3.eq(1).to_numpy() & df1.eq(0).to_numpy()).astype(int)
print (df)
   b1  b2  b20
0   1   0    0
1   0   0    0
2   0   0    0
3   0   0    0
4   1   0    0
5   0   0    0

The code below can give you the indices that satisfy the conditions for all three dataframes for each iteration in columns,

[{f"{i}": list(filter(lambda item:(a["a1"][item] == 1) & (b[f"b{i}"][item] == 1) & (c[f"c{i}"][item] == 1), range(100)))} for i in range(1, 21)]

Subset of the output -

[{'1': [1, 9, 14, 18, 23, 24, 28, 33, 45, 51, 53, 77, 88, 89, 90]},
 {'2': [5, 17, 27, 32, 44, 56, 73, 76, 79, 88, 89, 92]},
 {'3': [9, 14, 24, 25, 43, 46, 53, 55, 73, 76, 86, 91, 92, 94, 96]},
 {'4': [17, 20, 22, 24, 26, 34, 46, 65, 73, 76, 77, 81, 88, 92]},
 {'5': [9, 17, 23, 34, 36, 44, 48, 49, 75, 76, 88, 91, 94, 95]}]

I had fun solving this question. Thanks for asking it!

@new2cod3 You did not use @jezrael's code correctly.

(1) Do not put the column names into the solution. For example, do not put 'a1' between df1 and eq()

(2) Make sure the first df is one of the 20 column dataframes

(3) You need to convert the other two dataframes into a numpy array to be able to run the logical operation.

For clarity:

If the NAME of the dataframes are A (1 x 100), B (20 x 100) and C (20 x 100), then the code should look like this:

df = ( B.eq(1) & C.eq(1).to_numpy() & A.eq(0).to_numpy() ).astype(int)

If you need to change the values you want to compare then just change the values inside the eq(n) method. This code worked fine for me. Thanks

Related