Search code examples
pandasdataframenumpypandas-groupbydata-cleaning

determine maximum value of each row with the column name in Python


I have a table which's been attached. The table includes a different number of model of cars with their EU class (columns are a number of models in each class). I am trying to identify the maximum value of each model (at each row) by identifying the EU class (column name) in Python. for example the first row the maximum number goes to Euro 5 with 3677 cars whose model of vehicle is 320 GH. I tried different commands such as

maxValuesObj = D_high_model_EUstd.loc[D_high_model_EUstd['Model of Vehicle'].idxmax()]

but faced this error "reduction operation 'argmax' not allowed for this type". I was wondering if anybody can help or suggest to me any solution. Thanks enter image description here


Solution

  • You can use pd.idxmax

    For example:

    df.drop(columns="Model of Vehicle").idxmax(axis=1) # Drop text column since idxmax don't work with them
    

    Or create another dataframe:

    pd.concat({
        "Model of Vehicle": df["Model of Vehicle"],
        "Max":df.drop(columns="Model of Vehicle").idxmax(axis=1)
    },axis=1)