DataFrame 列的所有可能组合 - pandas / python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11435668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
All possible combinations of columns of a DataFrame - pandas / python
提问by gustavopr
Given a DataFrame that contains multiple columns (possible regressors), how can I generate all possible combinations of columns to test them into different regressions? I'm trying to select the best regression model from all the possible combination of regressors.
给定包含多个列(可能的回归量)的 DataFrame,如何生成所有可能的列组合以将它们测试为不同的回归?我正在尝试从所有可能的回归量组合中选择最佳回归模型。
For example, I have this DataFrame:
例如,我有这个DataFrame:
A B
1/1/2011 1 4
1/2/2011 2 5
1/3/2011 3 6
and I want to generate the following ones:
我想生成以下内容:
A B
1/1/2011 1 4
1/2/2011 2 5
1/3/2011 3 6
A
1/1/2011 1
1/2/2011 2
1/3/2011 3
B
1/1/2011 4
1/2/2011 5
1/3/2011 6
回答by archlight
If you are looking for combination of columns to regression against each other
如果您正在寻找相互回归的列组合
df = DataFrame(numpy.random.randn(3,6), columns=['a','b','c','d','e','g'])
df2 =[df[list(pair)] for pair in list(iter.combinations(df.columns, 2))]
回答by Chang She
Try using itertoolsto generate the powerset of column names:
尝试使用itertools生成列名的 powerset:
In [23]: import itertools as iter
In [24]: def pset(lst):
....: comb = (iter.combinations(lst, l) for l in range(len(lst) + 1))
....: return list(iter.chain.from_iterable(comb))
....:
In [25]: pset(lst)
Out[25]:
[(),
('A',),
('B',),
('C',),
('D',),
('A', 'B'),
('A', 'C'),
('A', 'D'),
('B', 'C'),
('B', 'D'),
('C', 'D'),
('A', 'B', 'C'),
('A', 'B', 'D'),
('A', 'C', 'D'),
('B', 'C', 'D'),
('A', 'B', 'C', 'D')]

