Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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]

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  • 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

>Solution :

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading