當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas dataframe.equals()用法及代碼示例

Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas dataframe.equals()函數用於確定所考慮的兩個數據幀對象是否相等。不像dataframe.eq()方法,運算結果是一個標量布爾值,指示 DataFrame 對象是否相等。

用法: DataFrame.equals(other)

參數:
other: DataFrame

返回:標量:布爾值

範例1:采用equals()函數查找兩個不同 DataFrame 對象之間的比較結果。

# 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


讓我們找到兩個 DataFrame 之間的比較結果。

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

輸出:

輸出為False,因為兩個數據幀彼此不相等。它們具有不同的元素。

範例2:采用equals()用於測試兩個 DataFrame 對象之間是否相等的函數NaN值。
注意:相同位置的NaN被認為是相等的。

# 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


讓我們對兩個 DataFrame 執行比較操作。

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

輸出:

輸出標量布爾值。 True表示兩個數據幀在相應的單元格中具有相等的值。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.equals()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。