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

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

Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.

Pandas Series.equals() function test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements.

Syntax: Series.equals(other)

Parameter :
other : The other Series or DataFrame to be compared with the first.

Returns : True if all elements are the same in both objects, False otherwise.

Example #1: Use Series.equals() function to check whether the underlying data in the two given series objects are same or not.

# importing pandas as pd
import pandas as pd
  
# Creating the first Series
sr1 = pd.Series([80, 25, 3, 25, 24, 6])
  
# Creating the second Series
sr2 = pd.Series([80, 25, 3, 80, 24, 25])
  
# Create the Index
index_ = ["Coca Cola", "Sprite", "Coke", "Fanta", "Dew", "ThumbsUp"]
  
# set the first series index
sr1.index = index_
  
# set the second series index
sr2.index = index_
  
# Print the first series
print(sr1)
  
# Print the second series
print(sr2)

Выход :

Now we will use Series.equals() function to check if the underlying data in the two given series objects are same or not.

# check for equality
result = sr1.equals(other = sr2)
  
# Print the result
print(result)

Выход :


As we can see in the output, the Series.equals() function has returned False indicating the element in the two given series objects are not same.
 
Example #2: Use Series.equals() function to check whether the underlying data in the two given series objects are same or not.

# importing pandas as pd
import pandas as pd
  
# Creating the first Series
sr1 = pd.Series([80, 25, 3, 25, 24, 6])
  
# Creating the second Series
sr2 = pd.Series([80, 25, 3, 25, 24, 6])
  
# Create the Index
index_ = ["Coca Cola", "Sprite", "Coke", "Fanta", "Dew", "ThumbsUp"]
  
# set the first series index
sr1.index = index_
  
# set the second series index
sr2.index = index_
  
# Print the first series
print(sr1)
  
# Print the second series
print(sr2)

Выход :

Now we will use Series.equals() function to check if the underlying data in the two given series objects are same or not.

# check for equality
result = sr1.equals(other = sr2)
  
# Print the result
print(result)

Output :

As we can see in the output, the Series.equals() function has returned True indicating the element in the two given series objects are same.

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

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