Dinesh Beura
Dinesh Beura

Reputation: 171

Find the max from each row in Python

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

Upvotes: 13

Views: 31841

Answers (1)

U3.1415926
U3.1415926

Reputation: 900

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]

Upvotes: 23

Related Questions