如何在python中使用True/False语句测试所有可能的组合? [英] How to test all possible combinations with True/False Statement in python?
问题描述
我有两个DataFrame,每个列都包含True/False语句.我正在寻找一种方法来测试所有可能的组合,并找出df1中每一行的"True"在df2中相应行中的"True"在哪里.
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:
对于每行,从"Main1"列开始,测试行是否等于True,以及"Sub1"列中的行是否也为True.接下来,测试"Main1"中的行是否等于true,以及"Sub1"列中的行是否为True,而"sub2"列中的行是否也为True.在这种情况下,如果所有值均为True,则输出将为True.然后对所有列和所有可能的组合重复.
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!
推荐答案
您可以在 pandas 中进行识别的功能所考虑的组合中所有列均等于 True
的行.我在下面提供了一个示例,其中考虑了2列或3列的所有组合.
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
这篇关于如何在python中使用True/False语句测试所有可能的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!