Search code examples
pythonpandasdataframeappendrows

Create a pandas DataFrame from the cartesian product of lists


I have some python code that runs a simple for loop and prints out every combination of results, and I am trying to figure out how to append this all to a single dataframe, based on the order the results are produced in. I will explain below.

I have the following code:

categories = ['small', 'medium', 'big']
parameters = ['p1_5_p2_4_p3_2', 'p1_3_p2_8_p3_3', 'p1_4_p2_3_p3_6']
Blue = [5, 4, 3]

for parameter in parameters:
    for category in categories:
        for x in Blue:
            y = x + 1
            z = x + 2
            
            print(category)
            print(parameter)
            print(y)
            print(z)
            print('')

which produces:

small
p1_5_p2_4_p3_2 
6 
7

small 
p1_5_p2_4_p3_2 
5 
6

small 
p1_5_p2_4_p3_2 
4 
5

medium 
p1_5_p2_4_p3_2 
6 
7

medium 
p1_5_p2_4_p3_2 
5 
6

medium 
p1_5_p2_4_p3_2 
4 
5

big 
p1_5_p2_4_p3_2 
6 
7

big 
p1_5_p2_4_p3_2 
5 
6

big
p1_5_p2_4_p3_2 
4 
5 

small
p1_3_p2_8_p3_3
6 
7
...

Is there a way to just send this to a pandas dataframe so that the dataframe looks like:

Category      Parameters         Value_1    Value_2
----------------------------------------------------
small         p1_5_p2_4_p3_2           6          7 
small         p1_5_p2_4_p3_2           5          6
small         p1_5_p2_4_p3_2           4          5
medium        p1_5_p2_4_p3_2           6          7
medium        p1_5_p2_4_p3_2           5          6
medium        p1_5_p2_4_p3_2           4          5
big           p1_5_p2_4_p3_2           6          7
big           p1_5_p2_4_p3_2           5          6
big           p1_5_p2_4_p3_2           4          5
small         p1_3_p2_8_p3_3           6          7   
...  

Is there a way to organize my initial outputs into this dataframe?


Solution

  • itertools.product is the most pythonic way to do this. However, if you want to use the code you already have, you're almost right there

    #create a list to append your values into
    data=[]
    
    categories = ['small', 'medium', 'big']
    parameters = ['p1_5_p2_4_p3_2', 'p1_3_p2_8_p3_3', 'p1_4_p2_3_p3_6']
    Blue = [5, 4, 3]
    
    for parameter in parameters:
        for category in categories:
            for x in Blue:
                y = x + 1
                z = x + 2
    
                #append instead of printing
                row=[category,parameter,y,z]
                data.append(row)
    
    #create your dataframe
    my_df=pd.DataFrame(columns=['Category','Parameters','Value_1','Value_2'], data=data)
    
      Category  Parameters      Value_1 Value_2
    0   small   p1_5_p2_4_p3_2  6       7
    1   small   p1_5_p2_4_p3_2  5       6
    2   small   p1_5_p2_4_p3_2  4       5
    3   medium  p1_5_p2_4_p3_2  6       7
    4   medium  p1_5_p2_4_p3_2  5       6