简体   繁体   中英

All possible combinations of columns of a DataFrame - pandas / python

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.

For example, I have this 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

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))]

Try using itertools to generate the powerset of column names:

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')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM