1

I have an array "station" that contains two values [28450., 29980.]. These represent station names. I want to get a count of these stations in a new variable using numpy:

station_count = np.unique(station)
print("The number of unique stations is ", station_count)

gives me:

The number of unique stations is [28450. 29980.]

How do I get a number 2 instead of [28450. 29980.]. I have tried count but I don't seem to be getting the syntax correctly.

mkw
  • 123
  • 2
  • 10
  • Possible duplicate of [Efficiently counting number of unique elements - NumPy / Python](https://stackoverflow.com/questions/46575364/efficiently-counting-number-of-unique-elements-numpy-python) – fuglede Jun 29 '19 at 08:12

2 Answers2

2

You can use the len of the unique array:

station_count = len(np.unique(station))
print("The number of unique stations is ", station_count)
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
0

You can get more information by code given below:-

import numpy as np

x = np.array([1,1,1,2,2,2,5,25,1,1])
unique, counts = np.unique(x, return_counts=True)
print(f"The number of unique values are \t {len(unique)}")
print()
for value, number in zip(unique,counts):
    print(f"The value {value} repeats \t -->> {number} times")

I hope you learn something new, related to your question.

Rahul charan
  • 765
  • 7
  • 15