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

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

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas dataframe.equals() function is used to determine if two dataframe object in consideration are equal or not. Unlike dataframe.eq() method, the result of the operation is a scalar boolean value indicating if the dataframe objects are equal or not.

Syntax: DataFrame.equals(other)

Parameters:
other : DataFrame

Returns: Scalar : boolean value

Example #1: Use equals() function to find the result of comparison between two different dataframe objects.

# importing pandas as pd
import pandas as pd
  
# Creating the first dataframe 
df1 = pd.DataFrame({"A":[1,5,7,8],
                  "B":[5,8,4,3],
                  "C":[10,4,9,3]})
  
# Creating the second dataframe
df2 = pd.DataFrame({"A":[5,3,6,4],
                  "B":[11,2,4,3],
                  "C":[4,3,8,5]})
  
# Print the first dataframe
df1
  
# Print the second dataframe
df2


Let’s find the result of comparison between both the data frames.

# To find the comparison result
df1.equals(df2)

Выход :

Результатом будет False, потому что два фрейма данных не равны друг другу. В них есть разные элементы.

Example #2: Use equals() function to test for equality between two data frame object with NaN values.
Note : NaNs in the same location are considered equal.

# importing pandas as pd
import pandas as pd
  
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1,2,3],
                  "B":[4,5,None],
                  "C":[7,8,9]})
  
# Creating the second dataframe
df2 = pd.DataFrame({"A":[1,2,3],
                  "B":[4,5,None],
                  "C":[7,8,9]})
  
# Print the first dataframe
df1
  
# Print the second dataframe
df2


Let’s perform comparison operation on both the dataframes.

# To find the comparison between two dataframes
df1.equals(df2)

Выход :

Выходное скалярное логическое значение. Истина указывает, что оба фрейма данных имеют равные значения в соответствующих ячейках.

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

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