是否可以将 numpy.select() 与 DataFrame 一起使用,其中选择列表取决于一个或多个列

Is it possible to use numpy.select() with a DataFrame where the choicelist depends on one or more column

我正在尝试在我的数据框上使用 numpy.select() 创建一个数组,我想知道是否可以使我的选择列表成为变量。例如这里是我的 DataFrame:

   face matériau   profilé  ext1_1  ext1_2  ext2_1  ext2_2  longeur  cl
0     1     A588  UPN 80/0       0       0       5       5    7.071   3
1     0     A514    T 50/2       0       0       5       5    7.071   2
2     2     A514    T 80/2       1       2       5       4    7.071   2

conditions = [df['face'] == 0, df['face'] == 1, df['face'] == 2, df['face'] == 3, df['face'] == 4, df['face'] == 5]

outputs1 = [
                [df.loc[df.index]['ext1_1'], 0, df.loc[df.index]['ext1_2']],
                [0, df.loc[df.index]['ext1_1'], df.loc[df.index]['ext1_2']],
                [x_max, df['ext1_1'], df.loc[df.index]['ext1_2']],
                [df.loc[df.index]['ext1_1'], y_max, df.loc[df.index]['ext1_2']],
                [df.loc[df.index]['ext1_1'], df.loc[df.index]['ext1_2'], z_max],
                [df.loc[df.index]['ext1_1'], df.loc[df.index]['ext1_2'], 0]
                ]

array= np.select(conditions, outputs1)

例如:

我的代码抛出错误:ValueError: shape mismatch: objects cannot be broadcast to a single shape 所以我认为我的选择列表有问题

您可以使用广播:

a = df['face'].to_numpy()[ None, :]

a11 = df['ext1_1'].to_numpy()
a12 = df['ext1_2'].to_numpy()

conditions = [a == 0, a == 1, a == 2, a == 3, a == 4, a == 5]

x_max, y_max, z_max = 100,200,300

outputs1 = [
                [a11, [0] *len(df), a12],
                [[0] *len(df), a11, a12],
                [[x_max] *len(df), a11, a12],
                [a11, [y_max] *len(df), a12],
                [a11, a12, [z_max] *len(df)],
                [a11, a12, [0] *len(df)]
                ]

array = np.select(conditions, outputs1).T

print (array)
[[  0   0   0]
 [  0   0   0]
 [100   1   2]]