I have a pandas dataframe with 0 rows and 58 columns, but for the sake of illustration let's assume I have a dataframe with 0 rows and 3 columns that looks like this:
col1 | col2 | col3 |
---|
I need to expand this table to represent all possible combinations of one-hot-encoded vectors like this:
col1 | col2 | col3 |
---|---|---|
0 | 0 | 0 |
1 | 1 | 1 |
0 | 1 | 0 |
1 | 1 | 0 |
1 | 0 | 0 |
0 | 0 | 1 |
0 | 1 | 1 |
How can I do this in python?
CodePudding user response:
You can use itertools.product
:
from itertools import product
pd.DataFrame(list(product(*([[0,1]]*len(df.columns))) ), columns=df.columns)
Output:
col1 col2 col3
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1