从列表的笛卡尔积创建一个 pandas DataFrame

Create a pandas DataFrame from the cartesian product of lists

我有一些 python 代码可以运行一个简单的 for 循环并打印出每个结果组合,我试图弄清楚如何根据结果是在里面产生的。我会在下面解释。

我有以下代码:

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('')

产生:

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
...

有没有办法将其发送到 pandas 数据框,使数据框看起来像:

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   
...  

有没有办法将我的初始输出组织到这个数据框中?

您可以使用 itertools.product:

from itertools import product

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]

df = pd.DataFrame(
    product(categories, parameters, np.array(Blue) + 1, np.array(Blue) + 2),
    columns=["Category", "Parameters", "Value_1", "Value_2"],
)
print(df)

打印:

   Category      Parameters  Value_1  Value_2
0     small  p1_5_p2_4_p3_2        6        7
1     small  p1_5_p2_4_p3_2        6        6
2     small  p1_5_p2_4_p3_2        6        5
3     small  p1_5_p2_4_p3_2        5        7
4     small  p1_5_p2_4_p3_2        5        6

...

itertools.product 是执行此操作的最pythonic 方法。但是,如果您想使用已有的代码,几乎就可以了

#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