How do I find the max value in only specific columns in a row?

Question:

If this was my dataframe

a b c
12 5 0.1
9 7 8
1.1 2 12.9

I can use the following code to get the max values in each row… (12) (9) (12.9)

df = df.max(axis=1)

But I don’t know would you get the max values only comparing columns a & b (12, 9, 2)

Asked By: Jody

||

Answers:

Assuming one wants to consider only the columns a and b, and store the maximum value in a new column called max, one can do the following

df['max'] = df[['a', 'b']].max(axis=1)

[Out]:

      a  b     c   max
0  12.0  5   0.1  12.0
1   9.0  7   8.0   9.0
2   1.1  2  12.9   2.0

One can also do that with a custom lambda function, as follows

df['max'] = df[['a', 'b']].apply(lambda x: max(x), axis=1)

[Out]:

      a  b     c   max
0  12.0  5   0.1  12.0
1   9.0  7   8.0   9.0
2   1.1  2  12.9   2.0

As per OP’s request, if one wants to create a new column, max_of_all, that one will use to store the maximum value for all the dataframe columns, one can use the following

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


[Out]:

      a  b     c   max  max_of_all
0  12.0  5   0.1  12.0        12.0
1   9.0  7   8.0   9.0         9.0
2   1.1  2  12.9   2.0        12.9
Answered By: Gonçalo Peres
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.