How to create all column combinations for multiple variables in pandas?

For a given range for n variables. As an example, I took n = 3.

A : [1,3] B: [5,10,12] C: [100,113] 

Please note that values ​​in the above range may also float.

How can we create a dataframe where each column is a unique combination of input variables?

  c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 a 1 1 1 3 3 3 1 1 1 3 3 3 b 5 10 12 5 10 12 5 10 12 5 10 12 c 100 100 100 100 100 100 113 113 113 113 113 113 
+1
source share
1 answer

Using Itertools.product, I can do all the combinations of the list, after which you can write each combination to your DataFrame

 import itertools A = [1, 3] B = [5, 10, 12] C = [100, 113] a = [A, B, C] print(list(itertools.product(*a))) # Outputs [(1, 5, 100), (1, 5, 113), (1, 10, 100), (1, 10, 113), (1, 12, 100), (1, 12, 113), (3, 5, 100), (3, 5, 113), (3, 10, 100), (3, 10, 113), (3, 12, 100), (3, 12, 113)] 

 idx = ['c{}'.format(i) for i in range(1, len(data)+1)] data = list(itertools.product(*a)) df = pd.DataFrame(data, index=idx, columns=list('abc')).T df c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 a 1 1 1 1 1 1 3 3 3 3 3 3 b 5 5 10 10 12 12 5 5 10 10 12 12 c 100 113 100 113 100 113 100 113 100 113 100 113 
+4
source

Source: https://habr.com/ru/post/1273832/


All Articles