1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
所以,我并不為這個(gè)感到驕傲,但也許它有機(jī)會(huì)獲勝 :)......
我認(rèn)為你需要離開數(shù)據(jù)幀,因?yàn)樗荒荛L(zhǎng)得足夠大,無(wú)法正確保存你的結(jié)果。如果您的結(jié)果可預(yù)測(cè)地稀疏,則可以使用替代結(jié)構(gòu),如下所示。
請(qǐng)注意,對(duì)于您正在做的事情來(lái)說(shuō),這將是一個(gè)很長(zhǎng)的循環(huán),22B x數(shù)據(jù)幀的長(zhǎng)度,因此超過(guò)一萬(wàn)億次命中,但是如果您只需要做一次,誰(shuí)在乎。中的組合函數(shù)是一個(gè)生成器,因此它將具有內(nèi)存效率。itertools
我認(rèn)為您正在尋找上面“全部為 True”的結(jié)果,因?yàn)槟谑褂卯a(chǎn)品運(yùn)算符。我在評(píng)論中說(shuō)錯(cuò)了。
如果完成,您可以在下面添加第二個(gè)循環(huán)來(lái)覆蓋大小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]
添加回答
舉報(bào)