How to test all possible combinations with True/False Statement in python?

柔情痞子 提交于 2021-02-11 13:58:51

问题


I have two DataFrames where each column contain True/False statements. I am looking for a way to test all possible combinations and find out where "True" for each row in df1 also is "True" in the corresponding row in df2.

In reference to the data below, the logic would be something like this:

For each row, starting in column "Main1", test if row is equal to True and if row in column "Sub1" also is True. Next, test if row in "Main1" is equal to true and if rows in column "Sub1" is True and column "sub2" also is True. In this case, if all values are True, the output would be True. Then repeat for all columns and all possible combinations.

df1:

   Main1  Main2  Main3
0   True  False   True
1  False  False  False
2  False   True   True
3  False  False   True
4  False   True   True
5   True   True   True
6   True  False  False

df2:

    Sub1   Sub2   Sub3
0  False  False   True
1  False   True  False
2   True  False   True
3  False  False  False
4   True   True  False
5  False  False  False
6   True   True   True

The output would be similar to something like this.

Of course, I could do this manually but it would be timely as well as there would be rooms for errors.

   Main1Sub1  Main1Sub1Sub2  ...  Main3Sub2Sub3  Main3Sub3
0      False          False  ...          False       True
1      False          False  ...          False      False
2      False          False  ...          False       True
3      False          False  ...          False      False
4      False          False  ...          False      False
5      False          False  ...          False      False
6       True           True  ...          False      False

[7 rows x 18 columns]

Any help on how to tackle this problem is appreciated!


回答1:


You can use the combinations() function in itertools to extract all the possible combinations of the columns of the 2 data frames, and then use the product() function in pandas to identify the rows where all the columns in the considered combination are equal to True. I included an example below, which considers all combinations of either 2 or 3 columns.

import pandas as pd
from itertools import combinations

df1 = pd.DataFrame({"Main1": [True, False, False, False, False, True, True],
                    "Main2": [False, False, True, False, True, True, False],
                    "Main3": [True, False, True, True, True, True, False]})

df2 = pd.DataFrame({"Sub1": [False, False, True, False, True, False, True],
                    "Sub2": [False, True, False, False, True, False, True],
                    "Sub3": [True, False, True, False, False, False, True]})

df3 = df1.join(df2)

all_combinations = list(combinations(df3.columns, 2)) + \
                   list(combinations(df3.columns, 3))

for combination in all_combinations:

   df3["".join(list(combination))] = df3[list(combination)].product(axis=1).astype(bool)

df3.drop(labels=["Main1", "Main2", "Main3", "Sub1", "Sub2", "Sub3"], axis=1, inplace=True)

df3

   Main1Main2  Main1Main3  ...  Main3Sub2Sub3  Sub1Sub2Sub3
0       False        True  ...          False         False
1       False       False  ...          False         False
2       False       False  ...          False         False
3       False       False  ...          False         False
4       False       False  ...          False         False
5        True        True  ...          False         False
6       False       False  ...          False          True



来源:https://stackoverflow.com/questions/61027875/how-to-test-all-possible-combinations-with-true-false-statement-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!