I have this data
product color size
p1 Red XXL
p2 Blue XL
p3 L
S
I want to make combinations from the columns as follows:
p1, Red, XXL
p1, Red, XL
.
.
p3, Blue, S
I tried make all columns in one list then use itertools.combinations but the result contains some unwanted data like:
p1, p2, p3 OR Red, Blue, XXL OR XXL, XL, S ....
my code is:
df = read_csv('./GenerateProducts.csv', delimiter=',')
df_columns = df.columns.tolist()
list_data = DataFrame()
for i in df_columns:
list_data = concat([list_data,df[i].dropna(axis=0)])
generated_products = DataFrame( combinations( list_data[0] ,len(df_columns) ) )
I am also trying to make it dynamic I tried to make the columns into dict then use keys as pointer for the data but I did not know how to implement this logic, my experience with dict is too shallow
data = dict()
for i in df_columns:
data[i] = df[i].dropna(axis=0)
I read a bout the itertools.product and that is why I made the dict also to use for loop to make same changes using the dict keys.
I think my execution with dict got me to confuse myself, any guidance
EDIT:
I got it to work
temp = []
for i in df_columns:
temp += [data[i]]
final_df = DataFrame(product(*temp), columns=df_columns)
final_df
I am wondering is there more efficient way to accomplish the same result
Thank you