9

How to find the max from each row in Python and store it in a NumPy array or Pandas DataFrame and store it in a NumPy array, i.e. the output below?

0.511474    0.488526
0.468783    0.531217
0.35111     0.64889
0.594834    0.405166

Output:

0.511474
0.531217
0.64889
0.594834
2
  • are the rows lists, df etc?
    – Joe
    Aug 8, 2018 at 6:53
  • 2
    df.max(axis=1).values
    – Alexander
    Aug 8, 2018 at 6:55

1 Answer 1

18

Use the numpy amax function. np.amax

import numpy as np
a = np.array([[0.511474,    0.488526],
            [0.468783,    0.531217],
            [0.35111,     0.64889],
            [0.594834,    0.405166]])
# axis=1 to find max from each row
x = np.amax(a, axis=1)
print(x)

which returns:

[0.511474 0.531217 0.64889  0.594834]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.