Python Pandas – 找出两个数据帧之间的差异
要找到两个DataFrame之间的差异,您需要检查其是否相等。另外,检查列的相等性。
让我们用两列创建DataFrame1-
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
创建具有两列的DataFrame2-
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
检查特定列“单位”的相等性-
dataFrame2['Units'].equals(dataFrame1['Units'])
检查两个数据帧的相等性-
Are both the DataFrames equal?",dataFrame1.equals(dataFrame2)
示例
以下是代码-
import pandas as pd #CreateDataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame1 ...\n",dataFrame1 #CreateDataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"\nDataFrame2 ...\n",dataFrame2 #checkforspecificcolumnUnitsequality print"\nBoth the DataFrames have similar Units column? ",dataFrame2['Units'].equals(dataFrame1['Units']) #checkforequality print"\nAre both the DataFrames equal? ",dataFrame1.equals(dataFrame2)输出结果
这将产生以下输出-
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 DataFrame2 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 Both the DataFrames have similar Units column? True Are both the DataFrames equal? True