DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

NumPy unique: Values, Counts and Unique Rows

Use np.unique with return_counts for frequencies, axis=0 for unique rows, and return_inverse to rebuild an array, with notes on NumPy 2.x changes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To get distinct values and how often each one appears in a NumPy array, call values, counts = np.unique(a, return_counts=True). To get distinct rows of a 2D array, call np.unique(a, axis=0), and use axis=1 for distinct columns. The outputs are aligned by position, so the details below matter mainly when you need to map results back to the original data or write code that runs across NumPy versions.

Getting unique values and their counts

With the default axis=None, np.unique flattens the input and returns the distinct scalar values in sorted order. Adding return_counts=True returns a second array of occurrence counts. The two arrays line up position by position: counts[i] is the number of times values[i] appears in the input, whatever the input’s shape.

As an Amazon Associate I earn from qualifying purchases.

import numpy as np

a = np.array([3, 1, 2, 3, 1, 1])
values, counts = np.unique(a, return_counts=True)
print(values)  # [1 2 3]
print(counts)  # [3 1 2]

Here the value 1 appears three times, 2 once, and 3 twice. A 2D input works the same way without any extra flags: it is flattened first, so every element counts individually. If you want counts per row rather than per element, use the row-based form in the next section.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Unique rows and unique columns

To treat each row as one item, pass axis=0. NumPy compares whole rows and returns them sorted lexicographically. Combine it with return_counts=True to count how many times each distinct row occurs:

b = np.array([[1, 2],
              [3, 4],
              [1, 2]])

unique_rows, row_counts = np.unique(b, axis=0, return_counts=True)
print(unique_rows)  # [[1 2]
                    #  [3 4]]
print(row_counts)   # [2 1]

Use axis=1 to treat each column as one item instead. The result is the distinct columns, again sorted lexicographically. Two restrictions apply: object arrays are not supported with axis, and neither are structured arrays that contain objects. If your data holds Python objects, convert it to a numeric or string dtype first, or deduplicate it another way.

Tracking where values came from

Counts answer “how many,” but two other outputs answer “where.” Each is optional and is requested with a keyword flag. Together they let you locate representatives in the original array and rebuild it from the unique set.

First-occurrence indices with return_index

return_index=True returns, for each unique value, the index of its first occurrence in the input. This is useful when you want to keep the first record for each key, such as the first row that matches a given identifier.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
values, first_idx = np.unique(a, return_index=True)
print(first_idx)  # [1 2 0]

The value 1 first appears at index 1, 2 at index 2, and 3 at index 0.

Reconstructing the input with return_inverse

return_inverse=True returns an index array that maps each element of the original input to its position in the unique array. Indexing the unique array with it rebuilds the input:

unique_values, inverse = np.unique(a, return_inverse=True)
reconstructed = unique_values[inverse]
print(inverse)        # [2 0 1 2 0 0]
print(reconstructed)  # [3 1 2 3 1 1]

For a 1D array, this reconstruction is exact and keeps the original order. For axis-based deduplication, the mapping must be applied along the same axis. The NumPy reference documents np.take(unique, unique_inverse, axis=axis) for this case, and the shape behavior of the inverse output depends on the NumPy version, as described below.

Repeating values by their counts is not the same as reconstruction

You may be tempted to rebuild the data with np.repeat(values, counts). That produces the same multiset of values, but in sorted order. It does not preserve the original arrangement. If position matters, use the inverse indices instead.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Multidimensional inputs and the NumPy 2.0 change

NumPy 2.0 changed the shape of the inverse output for multidimensional inputs. The NumPy reference describes this change in its notes on np.unique. If your code must run on both older and newer releases, flatten the inverse array explicitly with inverse.reshape(-1) before using it for indexing. Confirm the expected shape against the NumPy version you are targeting, because a reshape that is correct in one release can be unnecessary in another.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

NaN handling and the sorted parameter

According to the current stable reference, equal_nan defaults to True, so repeated NaN values collapse into a single entry in the result. The parameter itself was introduced in NumPy 1.24.

The sorted parameter was added in NumPy 2.3. Its documented behavior with sorted=False is not a guarantee of any particular unsorted order: in practice the output may still come back sorted, and that behavior may change in future releases. If you need a specific order, sort the result yourself rather than relying on sorted=False.

Choosing the right call

Start by deciding what counts as one item: a scalar after flattening (default), a row (axis=0), or a column (axis=1). Then choose the extra outputs based on what you need back.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Goal Call What you get back
Distinct scalar values np.unique(a) Sorted array of unique values from the flattened input
Frequency of each value np.unique(a, return_counts=True) Unique values plus counts aligned by position
Distinct rows np.unique(a, axis=0, return_counts=True) Unique rows, sorted lexicographically, with row counts
Distinct columns np.unique(a, axis=1) Unique columns, sorted lexicographically
Representative locations np.unique(a, return_index=True) Index of the first occurrence of each unique value
Rebuild the original arrangement np.unique(a, return_inverse=True) Index map from each element to its unique value

Two cautions. Do not assume sorted=False yields a fixed unsorted order. And for multidimensional inverse results, check the shape against your NumPy version.

Official references

“

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.