search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | nanargmax method

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Numpy's `nanargmax(~)` method ignores all missing values (i.e. `NaN`s) returns the index that corresponds to the largest element in the array.

# Parameters

1. `a` | `array_like`

The input array.

2. `axis` | `int` | `optional`

The axis along which to compute the method. For 2D arrays, if `axis=0`, then the method is performed column-wise, and if `axis=1` then row-wise. If no axis is provided, then Numpy will deem your array as a flattened array.

# Return value

If no `axis` is provided, then a scalar is returned. Otherwise, a Numpy array is returned.

# Examples

## One-dimensional arrays

``` x = np.array([np.NaN,5,1,3])np.nanargmax(x) 1 ```

Here, 1 is returned because the largest value (i.e. 5) is located at index 1. In contrast, the `np.argmax(x)` method would return 0 since it considers `NaN` to be the largest.

## Two-dimensional arrays

Suppose we have the following 2D Numpy array:

``` x = np.array([[np.NaN,4],[1,3]])x array([[nan, 4.], [ 1., 3.]]) ```

### Max index of entire array

To obtain the index of the maximum value in the entire array, leave out the `axis` parameter:

``` np.nanargmax(x) 1 ```

### Max indices of every column

To obtain the index of the maximum values column-wise, set `axis=0`:

``` np.nanargmax(x, axis=0) array([1, 0]) ```

Here, we're going over each column of the matrix and computing the index of its largest value, while ignoring any missing values.

### Max indices of every row

To obtain the index of the maximum values row-wise, set `axis=1`:

``` np.nanargmax(x, axis=1) array([1, 1]) ```

Here, we're going over each row of the matrix and computing the index of its largest value. while ignoring any missing values.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!