from list of lists get max value with index and map index value to list of string with index

Viewed 34

I have list of lists & list , Initial need to find from list of lists argmax value with index and map list of lists index max value with another list

Input :

a = np.array([[9.8894545, 2.12012004, 2.1054542],
              [3.19212970, 9.6048260, 3.63252661],
              [9.97873928, 3.10315885, 2.12607185],
              [5.13391890, 4.53636282, 9.8429458]])
#type(a) -> np.ndarray
           
b = ['abc','def','ghi']
#type(b) -> list

Expected Output in dataframe:

               column_c  
                abc 
                def 
                abc 
                ghi 

I have tried below steps but couldn't map to another list

     a = np.array([[9.88945457e-01, 2.12012004e-11, 2.10545428e-02],
              [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
              [9.97873928e-01, 4.10315885e-12, 2.12607185e-03],
              [5.13391890e-07, 9.84294588e-01, 4.57048982e-02]])
     sam =np.max(a, axis=1)
     df_3 = pd.DataFrame(sam, columns = ['Column_A'])

Output(df_3)

       Column_A
       0    0.99
       1    0.96
       2    0.97
       3    0.98
1 Answers

update

if you want to index the list based on the index of the max, use:

a = np.array([[9.8894545, 2.12012004, 2.1054542],
              [3.19212970, 9.6048260, 3.63252661],
              [9.97873928, 3.10315885, 2.12607185],
              [5.13391890, 4.53636282, 9.8429458]])
       
b = ['abc','def','ghi']

df = pd.Series(np.array(b)[a.argmax(axis=1)])

output:

0    abc
1    def
2    abc
3    ghi
dtype: object

as DataFrame:

df_3 = pd.DataFrame({'column_c': np.array(b)[a.argmax(axis=1)]})

output:

  column_c
0      abc
1      def
2      abc
3      ghi

previous answer

IIUC, use:

a = np.array([[9.8894545, 2.12012004, 2.1054542],
              [3.19212970, 9.6048260, 3.63252661],
              [9.97873928, 3.10315885, 2.12607185],
              [5.13391890, 4.53636282, 9.8429458]])
       
b = ['abc','def','ghi']

df = pd.DataFrame(a.max(axis=0), index=b)

output:

            0
abc  9.978739
def  9.604826
ghi  9.842946
Related