Python | Pandas Index.nunique ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas Index.nunique() function return number of unique elements in the object. It returns a scalar value which is the count of all the unique values in the Index. By default the NaN values are not included in the count. If dropna parameter is set to be False then it includes NaN value in the count.
Syntax: Index.nunique(dropna=True)
Parameters :
dropna : Don’t include NaN in the count.Returns : nunique : int
Example #1: Use Index.nunique()() function to find the count of unique values in the Index. Do not include NaN values in the count.
# importing pandas as pdimport pandas as pd # Creating the indexidx = pd.Index(["Beagle", "Pug", "Labrador", "Pug", "Mastiff", None, "Beagle"]) # Print the Indexidx |
Выход :
Let’s find the count of unique values in the Index.
# to find the count of unique values.idx.nunique(dropna = True) |
Output :
As we can see in the output, the function has returned 4 indicating that there are only 4 unique values in the Index.
Example #2: Use Index.nunique() function find out all the unique values in the Index. Also include the missing values i.e. NaN values in the count.
# importing pandas as pdimport pandas as pd # Creating the indexidx = pd.Index(["Beagle", "Pug", "Labrador", "Pug", "Mastiff", None, "Beagle"]) # Print the Indexidx |
Выход :
Let’s find the count of unique values in the Index.
# to find the count of unique values.idx.nunique(dropna = False) |
Выход :
Как видно из выходных данных, функция вернула 5, что означает, что в индексе всего 5 уникальных значений. Мы также включили в подсчет недостающие значения.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.