在每一行找到最大值 - Pandas 数据框

Find the max value at each row - Pandas data frame

我有一个数据框,我想在其中找到 15 值之间每一行的最大值。

条件 1 到 5:列 -> A1、B1、C1、D1、E1、F1、G1、H1、J1

查找 Max_Value:列 -> A2、B2、C2、D2、E2、F2、G2、H2、J2

Max_Value(列)是屏幕截图上的预期输出。

您可以查看随附的屏幕截图以更好地理解问题。 在此先感谢您的支持!

import pandas as pd
df = pd.DataFrame({'Type': ['X', 'Y', 'Z'], 'ID': [1212, 2342, 2421], 
'A1': [1, 1, 1], 'A2': [0.3, 0.004, 0.86], 'B1': [2, 1.5, 3],       
'B2': [0.1, 0.8, 0.005], 'C1': [3, 2, 5], 'C2': [0.8, 0.2, 0.3],
'D1': [4, 2.5, 7], 'D2': [0.2, 0.4, 0.004], 'E1': [5, 3, 9],        
'E2': [0.9, 0.005, 0.99], 'F1': [6, 3.5, 11], 'F2': [0.4, 0.3, 0.4],
'G1': [7, 5, 13], 'G2': [0.6, 0.85, 0.003], 'H1': [8, 4.5, 15],     
'H2': [0.94, 0.088, 0.85], 'J1': [9, 5, 17], 'J2': [0.003, 0.0001,0.3]})
print(df)

您可以使用带有参数 axis=1pd.DataFrame.max 函数。

    import pandas as pd
    df = pd.DataFrame({'a': [2,4,6], 'b': [3,4,5], 'c': [1,7,2]})

    # Select first 5 columns
    df = df.iloc[: , :5]

    # Get max per row
    maxValuesObj = pd.DataFrame({'max': df.max(axis=1)})
    
    # Assign column values
    maxValuesObj['column'] = df.columns.values
    
    print(maxValuesObj)

        | max    | column
    0   | 3      | a
    1   | 7      | b
    2   | 6      | c

你能试试下面的代码吗:

mask = df.filter(regex=r'[A-Z]1').stack().between(1, 5).values
df['MAX'] = df.filter(regex=r'[A-Z]2').stack()[mask].groupby(level=0).max()

The expected result for the first row: 0,9 (1 and 5 values are between A1 and E2). The expected result for the second row: 0,85 (1 and 5 values are between A1 and J2). The expected result for the third row: 0,86 (1 and 5 values are between A1 and C2)

>>> df
  Type    ID  A1     A2  ...  J1      J2   MAX
0    X  1212   1  0.300  ...   9  0.0030  0.90
1    Y  2342   1  0.004  ...   5  0.0001  0.85
2    Z  2421   1  0.860  ...  17  0.3000  0.86

[3 rows x 21 columns]