Python | Pandas Index.duplicated ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas Index.duplicated() function Indicate duplicate index values. Duplicated values are indicated as True values in the resulting array. Either all duplicates, all except the first, or all except the last occurrence of duplicates can be indicated.
Syntax: Index.duplicated(keep=’first’)
Parameters :
keep : {‘first’, ‘last’, False}, default ‘first’
The value or values in a set of duplicates to mark as missing.
-> ‘first’ : Mark duplicates as True except for the first occurrence.
-> ‘last’ : Mark duplicates as True except for the last occurrence.
-> False : Mark all duplicates as True.Returns : numpy.ndarray
Example #1: Use Index.duplicated() function to indicate all the duplicated value in the Index except the first one.
# importing pandas as pdimport pandas as pd # Creating the Indexidx = pd.Index(["Labrador", "Beagle", "Labrador", "Lhasa", "Husky", "Beagle"]) # Print the Indexidx |
Выход :
Let’s find if a value present in Index is a duplicate value or unique.
# Identify the duplicated values except the firstidx.duplicated(keep ="first") |
Output :
As we can see in the output, the Index.duplicated() function has marked all the occurrence of duplicate value as True except the first occurrence.
Example #2: Use Index.duplicated() function to identify all the duplicate values. here all the duplicate values will be marked as True
# importing pandas as pdimport pandas as pd # Creating the Indexidx = pd.Index([100, 50, 45, 100, 12, 50, None]) # Print the Indexidx |
Выход :
Определим все повторяющиеся значения в индексе.
Note : We are having NaN values in the Index.
# Identify all duplicated occurrence of valuesidx.duplicated(keep = False) |
Выход :
The function has marked all the duplicate value as True. It has also treated the single occurrence of NaN value as unique and has marked it false.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.