简体   繁体   中英

How to determine the highest value from multiple columns using Pandas

The code below is example of how I have been attempting to find out what the highest value from multiple columns is, and then putting the highest value into a newly created column, using pandas.

def Alphabet (row):
    AlpFields = ["A", "B", "C", "D", "E", "F"]
    Alp = len(AlpFields)
    AlpCheck = row[:Alp]
    MaxValue = max(AlpCheck)
    if MaxValue == 0:
        return MaxValue, ""
    return MaxValue , AlpFields[AlpCheck.index(MaxValue)]
df.apply(lambda row: Alphabet(row), axis =1)
df['HighestAlphabetScore'] = df.apply(lambda row: Alphabet(row), axis =1)

In this case I am trying to find what the highest value across columns: A, B, C, D, E and F is and then placing this value in the newly created column "Highest Alphabet Score".

However when I run the code the following error comes up:

TypeError: ("'Index' object is not callable", u'occurred at index 0')

I have tried a variety of different things to try and fix the issue, but so far nothing has worked. Any help on what I am possibly doing wrong/any suggestions on how to get around this error would be really appreciated.

For your dataframe df you can call the max function for the column axis and assign the result to a new column.

Say that you need the maximum among only specific columns, then the code would be

df['HighestAlphabetScore'] = df[["A", "B", "C", "D", "E", "F"]].max(axis=1)

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