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输出结果# Create DataFrame1
dataFrame1 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90] }
)
print"DataFrame1 ...\n",dataFrame1
# Create DataFrame2
dataFrame2 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90]
}
)
print"\nDataFrame2 ...\n",dataFrame2
# check for specific column Units equality
print"\nBoth the DataFrames have similar Units column? ",dataFrame2['Units'].equals(dataFrame1['Units'])
# check for equality
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
以上是 Python Pandas – 找出两个数据帧之间的差异 的全部内容, 来源链接: utcz.com/z/317298.html