Skip to content
Advertisement

Using itertools for combinations in python

I have the following table with 4 columns:

JavaScript

My desired Output in a df:

JavaScript

I need it to loop through and combine all possible combinations. What is the best way to do this using python?

Advertisement

Answer

I assume that by table you mean a pandas dataframe, so the first step would be to collect the columns of interest into a list of lists:

JavaScript

Now if you have any list that contains elements other than strings, you need to convert them:

JavaScript

and finally use itertools to derive the product of these lists:

JavaScript

And the result should be similar to the one below:

JavaScript
Advertisement