Reputation: 209
I have a following list of dictionaries:
options = [{'A-1': ['x', 'y']},
{'A-3': ['x', 'y', 'z']},
Values of each dictionary (e.g. x
and y
) are basically the options that keys (e.g. A-1
) can have. How can I have the following dataframe of combinations? Only one value (e.g. either x
or y
) of a key (e.g. A-1
) can can take 1 at a time. All values of a dictionary cannot be 0 at a time.
I have trying to use itertools.combinations()
, but couldn't find the way to get the desired result.
This way I can find the number of combinations n_comb
and number of connections n_conn
which will be number of rows and columns of the dataframe.
n_conn = 0
n_comb = 1
for dic in options:
for key in dic:
n_comb = n_comb * len(dic[key])
n_conn = n_conn + len(dic[key])
Upvotes: 3
Views: 67
Reputation: 29742
One way using pandas.get_dummies
and merge
:
dfs = [pd.get_dummies(pd.DataFrame(o)).assign(merge=1) for o in options]
new_df = dfs[0].merge(dfs[1], on="merge").drop("merge", 1)
print(new_df)
Or make it more flexible using functools.reduce
:
from functools import reduce
new_df = reduce(lambda x, y: x.merge(y, on="merge"), dfs).drop("merge", 1)
Output:
A-1_x A-1_y A-3_x A-3_y A-3_z
0 1 0 1 0 0
1 1 0 0 1 0
2 1 0 0 0 1
3 0 1 1 0 0
4 0 1 0 1 0
5 0 1 0 0 1
Upvotes: 2