Python Pandas - 檢查 DataFrame 物件是否相等


要檢查 DataFrame 物件是否相等,請使用 equals() 方法。首先,讓我們建立具有兩列的 DataFrame1 −

dataFrame1 = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
      "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
   }
)

建立具有兩列的 DataFrame2 

dataFrame2 = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
      "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]

   }
)

要檢查 DataFrame 物件是否相等,請使用 equals() 方法

dataFrame1.equals(dataFrame2)

示例

程式碼如下

import pandas as pd

# Create DataFrame1
dataFrame1 = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
      "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]
   }
)

print"DataFrame1 ...\n",dataFrame1

# Create DataFrame2
dataFrame2 = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
      "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]

   }
)

print"\nDataFrame2 ...\n",dataFrame2

# check for equality
print"\nAre both the DataFrame objects equal? ",dataFrame1.equals(dataFrame2)

輸出

這將產生以下輸出

DataFrame1 ...
       Car   Reg_Price
0      BMW        7000
1    Lexus        1500
2     Audi        5000
3  Mustang        8000
4  Bentley        9000
5   Jaguar        6000

DataFrame2 ...
       Car   Reg_Price
0      BMW        7000
1    Lexus        1500
2     Audi        5000
3  Mustang        8000
4  Bentley        9000
5   Jaguar        6000

Are both the DataFrame objects equal? True

更新於:14-Sep-2021

175 次瀏覽

職業開端

完成課程獲得認證

開始學習
廣告