1 回答
TA贡献1829条经验 获得超7个赞
所以,我并不为这个感到骄傲,但也许它有机会获胜 :)......
我认为你需要离开数据帧,因为它不能长得足够大,无法正确保存你的结果。如果您的结果可预测地稀疏,则可以使用替代结构,如下所示。
请注意,对于您正在做的事情来说,这将是一个很长的循环,22B x数据帧的长度,因此超过一万亿次命中,但是如果您只需要做一次,谁在乎。中的组合函数是一个生成器,因此它将具有内存效率。itertools
我认为您正在寻找上面“全部为 True”的结果,因为您正在使用产品运算符。我在评论中说错了。
如果完成,您可以在下面添加第二个循环来覆盖大小2的组合!:)
import pandas as pd
from itertools import combinations
df = 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],
"Sub1": [False, False, True, False, True, False, True],
"Sub2": [False, True, False, False, True, False, True],
"Sub3": [True, False, True, False, False, False, True]})
print(df)
data = df.to_dict('index')
# test to see if it looks right for row 0
print(data[0])
# now the data is in a nested dictionary, which should be more "iterable"
results = []
for combo in combinations(df.columns, 3):
for key in data: # iterate through the rows in the data... index is key.
values = set(data[key][col] for col in combo)
if all(values):
results.append((key, combo))
# inspect results...
for result in results:
print(f'row: {result[0]} columns: {results[1]} product is TRUE')
收益 率:
Main1 Main2 Main3 Sub1 Sub2 Sub3
0 True False True False False True
1 False False False False True False
2 False True True True False True
3 False False True False False False
4 False True True True True False
5 True True True False False False
6 True False False True True True
{'Main1': True, 'Main2': False, 'Main3': True, 'Sub1': False, 'Sub2': False, 'Sub3': True}
row: 5 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 0 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 4 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 2 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
row: 6 columns: (0, ('Main1', 'Main3', 'Sub3')) product is TRUE
[Finished in 0.6s]
添加回答
举报