Python | Панды Series.drop_duplicates ()

Опубликовано: 27 Марта, 2022

Pandas Series.drop_duplicates() function returns a series object with duplicate values removed from the given series object.

Syntax: Series.drop_duplicates(keep=’first’, inplace=False)

Parameter :
keep : {‘first’, ‘last’, False}, default ‘first’
inplace : If True, performs operation inplace and returns None.

Returns : deduplicated : Series

Example #1: Use Series.drop_duplicates() function to drop the duplicate values from the given series object.

Выход :

Now we will use Series.drop_duplicates() function to drop the duplicate values in the underlying data of the given series object.

# drop duplicates
result = sr.drop_duplicates()
  
# Print the result
print(result)

Output :

As we can see in the output, the Series.drop_duplicates() function has successfully dropped the duplicate entries from the given series object.
 
Example #2 : Use Series.drop_duplicates() function to drop the duplicate values from the given series object.

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([11, 11, 8, 18, 65, 18, 32, 10, 5, 32, 32])
  
# Create the Index
index_ = pd.date_range("2010-10-09", periods = 11, freq ="M")
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

Output :

Now we will use Series.drop_duplicates() function to drop the duplicate values in the underlying data of the given series object.

# drop duplicates
result = sr.drop_duplicates()
  
# Print the result
print(result)

Output :

As we can see in the output, the Series.drop_duplicates() function has successfully dropped the duplicate entries from the given series object.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.