Counting and retrieving Unique elements from a pandas.Series object
The count() function returns the number of elements present in a pandas.series instance. The nunique() and unique() functions returns the unique count and unique elements. The python examples are provided for numeric, string and custom class object types.
The count() function returns the number of elements present in a pandas.Series instance.The NA/null/None values are not included in the count value.
The nunique() function returns the number of unique elements present in the pandas.Series.
The function Series.unique() returns the unique elements excluding the duplicate values present in a pandas.Series. Determining uniqueness of the elements is done through a hash table.
Example - Counting elements of a pandas.series
python
# Example Python program to count the number of
# elements present in a pandas.Series instance
import pandas as pds
cities = ["London", "Paris", "Madrid", "NewYork", "Chicago", "San Francisco"];
# Create a pandas.Series instance
series = pds.Series(cities);
print("Contents of the pandas.series:");
print(cities);
print("Number of elements present in the pandas.series:");
print(series.count());
Output
Output
Contents of the pandas.series:
['London', 'Paris', 'Madrid', 'NewYork', 'Chicago', 'San Francisco']
Number of elements present in the pandas.series:
6
Example-Count and get the unique elements present in a pandas.series
python
# Example Python program to get unique elements present in a pandas.Series
import pandas as pds
# Create a pandas.Series instance
numbers = pds.Series([1,2,3,5,7,3,5,7,3,5,7]);
print("Contents of the Series:");
print(numbers);
print("Number of Unique elements present in the Series:");
print(numbers.nunique());
print("Unique elements of the Series:");
print(numbers.unique());
Output
Output
Contents of the Series:
0 1
1 2
2 3
3 5
4 7
5 3
6 5
7 7
8 3
9 5
10 7
dtype: int64
Number of Unique elements present in the Series:
5
Unique elements of the Series:
[1 2 3 5 7]
Example- Get the unique class objects present in a pandas.series
python
# Example Python program to get unique elements present in a pandas.Series
import pandas as pds
class X:
def __init__(self, name):
self.name = name;
def __str__(self):
return self.name;
# Unique elements
x1 = X("x1");
x2 = X("x2");
x3 = X("x3");
# Copy
x4 = x1;
# Copy is not returned as identity of x1 and x4 are same
xSeries = pds.Series([x1, x2, x3, x4]);
uniques = xSeries.unique();
print("Unique objects:");
print(uniques);
# First and fourth elements have indeed different identities
xDashSeries = pds.Series([X("One"), X("Two"), X("Three"), X("One")]);
uniques = xDashSeries.unique();
print("Unique objects:")
print(uniques);
Output
Output
Unique objects:
[<__main__.X object at 0x1100db438> <__main__.X object at 0x1100db470>
<__main__.X object at 0x1100db4a8>]
Unique objects:
[<__main__.X object at 0x11c0a8da0> <__main__.X object at 0x11d4fe0b8>
<__main__.X object at 0x11d4fe048> <__main__.X object at 0x11d52a940>]
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on