user1471980
user1471980

Reputation: 10646

how do you select the maximum value accross each row in pandas

Hi have this data frame:

Server     1-Jun   6-Jun   1-jul  Jul-10
ServerA    8        9        5     90
ServerB    100      10      9     90

I need to create another column called maximumval and pick the maximum value from all of the months per Server:

Resulting data frame needs to be like this:

Server     1-Jun   6-Jun   1-jul  Jul-10.  maximumval
ServerA    8        9        5    90       90
ServerB    100      10      9     90       100

I tried this this

df['maximumval'] = df.max(axis=1)

I get this error:

'>=' no supported between instances of 'str' and 'float'

any ideas what does this mean? how do you fix this?

Upvotes: 1

Views: 59

Answers (2)

BeRT2me
BeRT2me

Reputation: 13241

If it makes sense, you may want to set the Server column as your index.

df = df.set_index("Server")
df['maximumval'] = df.max(axis=1)

Output:

         1-Jun  6-Jun  1-jul  Jul-10  maximumval
Server
ServerA      8      9      5      90          90
ServerB    100     10      9      90         100

Upvotes: 0

mozway
mozway

Reputation: 262214

This is due to the "Server" column that contains strings.

Use the numeric_only=True option of max:

df['maximumval'] = df.max(numeric_only=True, axis=1)

Alternatively, use select_dtypes to only keep the numeric columns:

df['maximumval'] = df.select_dtypes('number').max(axis=1)

Output:

    Server  1-Jun  6-Jun  1-jul  Jul-10  maximumval
0  ServerA      8      9      5      90          90
1  ServerB    100     10      9      90         100

Upvotes: 2

Related Questions