Pandas: find the maximum value in each row of a column and determine the corresponding values ​​in another column

I have two columns of a pandas dataframe for which the values ​​are lists of tuples such as:

df[‘A’].values
(1.55, 2.07, 2.20, 2.23)
(0.67, 1.10, 1.73, 1.35)
(2.92, 1.98, 2.30, 2.66)

      

and

df[‘B’].values
(1.55, 0.0086, 0.078, 0.12)
(0.672, 0.142, 0.0166, 0.0173)
(1.97, 0.0094, 0.1648, 0.016)

      

I would like to select the largest value for each row df['A']

and find the value at the appropriate position df['B']

to generate a new column like:

> df[‘C’]
0.12
0.0166
1.97

      

+3


source to share


1 answer


import pandas as pd
import numpy as np


df_dict = dict(A=[(1.55, 2.07, 2.20, 2.23), (0.67, 1.10, 1.73, 1.35), (2.92, 1.98, 2.30, 2.66)],
    B=[(1.55, 0.0086, 0.078, 0.12), (0.672, 0.142, 0.0166, 0.0173), (1.97, 0.0094, 0.1648, 0.016)])

df = pd.DataFrame(df_dict)

Out[180]: 
                         A                               B
0  (1.55, 2.07, 2.2, 2.23)     (1.55, 0.0086, 0.078, 0.12)
1  (0.67, 1.1, 1.73, 1.35)  (0.672, 0.142, 0.0166, 0.0173)
2  (2.92, 1.98, 2.3, 2.66)   (1.97, 0.0094, 0.1648, 0.016)

def apply_func(row):
    return row.B[np.array(row.A).argmax()]

df['C'] = df.apply(apply_func, axis=1)

Out[182]: 
                         A                               B       C
0  (1.55, 2.07, 2.2, 2.23)     (1.55, 0.0086, 0.078, 0.12)  0.1200
1  (0.67, 1.1, 1.73, 1.35)  (0.672, 0.142, 0.0166, 0.0173)  0.0166
2  (2.92, 1.98, 2.3, 2.66)   (1.97, 0.0094, 0.1648, 0.016)  1.9700

      



+7


source







All Articles