简体   繁体   中英

applying an if function in numpy array and put the results in another array

I have a dataframe which contains 3 columns and 100 rows, I converted this dataframe to numpy array, because i want it as a matrix, this array has the dimensions of (10,10,3) it means it is a (10*10) matrix,and each element inside the matrix has 3 values, I want to apply an if function in each element,and replace the element with the result of this function. this is how the dataframe looked like:

      fromlinkno   vianodeno   tolinkno
   0      15           1           16
   1      16           1           15
   2      25           2           26   
   3      16           3           17
   ..     ..           ..          ..
   95     44           43          28
   96     28           35          29
   [100 rows * 3 columns]

  import pandas as pd 
  import numpy as np

  df2=df.iloc[:100]
  arr=df2.to_numpy()
  arr.reshape(10,10,3)


 The array(matrix) looks like:
 [[[15   1   16]
   [16   1   15]
   [25   2   26]
   [16   3   17]
   [17   3   16]
   [17   4   18]
   [18   4   17]
   [18   5   19]
   [19   5   18]
   [19   6   20]]

  [[19   6   34]
   ............
   ............]
   ............
   ...........
   [44   43  28]
   [28   35   29]]

I want to apply this function for example: if (fromlinkno> tolinkno): if(tolinkno > vianodeno): return A elif(tolinkno vianodeno): return C elif(tolinkno < vianodeno):
return D

how can i do that?

the function i wrote is as an example, but the goal is to get another matrix which has letters instead of these element with 3 numbers, so it should look like( for example)

        [[A [C  .......... 
          B  D            ..
          A  A
          D  E            ..
          C  A
          E  B            .. 
          A  A
          A  B            ..
          D  C
          B] A]           E]]

You can achieve your output by using numpy where

To re-create your dataframe, I used random values:

df1 = pd.DataFrame(np.random.randint(1, 100, size=(100, 3)))

Reshaping it in the same size as you did:

array1 = df1.to_numpy()
array2 = array1.reshape(10, 10, 3)

Looping over each matrix and using nested numpy where:

for a in range(len(array2)):
    print(np.where(array2[a][:, 0] > array2[a][:, 2],
                        np.where(array2[a][:, 2] > array2[a][:, 1], "A",
                                 np.where(array2[a][:, 2] == array2[a][:, 1], "C",
                                          np.where(array2[a][:, 2] < array2[a][:, 1], "D", np.nan))), np.nan))

Note: np.nan is just a placeholder as an example.

Output:

['D' 'nan' 'nan' 'A' 'A' 'D' 'A' 'nan' 'nan' 'nan']
['nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'A' 'A' 'nan']
['nan' 'nan' 'D' 'A' 'D' 'D' 'A' 'C' 'A' 'nan']
['D' 'D' 'D' 'nan' 'A' 'D' 'C' 'D' 'nan' 'D']
['nan' 'D' 'nan' 'A' 'D' 'D' 'nan' 'D' 'nan' 'nan']
['D' 'nan' 'A' 'D' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan']
['nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'nan' 'A' 'A' 'D']
['D' 'nan' 'nan' 'A' 'nan' 'nan' 'nan' 'nan' 'nan' 'D']
['D' 'nan' 'D' 'D' 'nan' 'nan' 'nan' 'nan' 'nan' 'A']
['A' 'D' 'C' 'nan' 'D' 'A' 'nan' 'nan' 'D' 'A']

PS: This is just a print of the example outputs. You can create a new array of same dimensions as the original array and keep replacing elements where required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM