Home > Software engineering >  Is it possible to use numpy.select() with a DataFrame where the choicelist depends on one or more co
Is it possible to use numpy.select() with a DataFrame where the choicelist depends on one or more co

Time:03-07

I am trying to create an array using numpy.select() on my dataframe and I would like to know if it's possible to make my choicelist variable. For example here is my 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)

So for example:

  • if face == 1 I want as result : [0,0,0]

  • if face == 2 I want as result : [x_max, 1, 2] etc...

My code throw the error: ValueError: shape mismatch: objects cannot be broadcast to a single shape So i think there is a problem whith my choicelist

CodePudding user response:

You can use broadcasting:

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]]
  • Related