Python Pandas – 检查两个 DataFrame 的任何特定列是否相等
要检查两个 DataFrame 的任何特定列是否相等,请使用该equals()方法。让我们首先创建具有两列的 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', 'Mercedes', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90]
}
)
检查特定列 Units 的相等性 -
dataFrame2['Units'].equals(dataFrame1['Units'])
示例
以下是代码 -
import pandas as pd输出结果# 创建 DataFrame1
dataFrame1 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90] }
)
print"DataFrame1 ...\n",dataFrame1
# 创建 DataFrame2
dataFrame2 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90]
}
)
print"\nDataFrame2 ...\n",dataFrame2
# 检查是否相等
print"\nAre both the DataFrame objects equal? ",dataFrame1.equals(dataFrame2)
# 检查特定列的单位相等
print"\nAre both the DataFrames have similar Units column? ",dataFrame2['Units'].equals(dataFrame1['Units']
)
这将产生以下输出 -
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
Are both the DataFrame objects equal? True
Are both the DataFrames have similar Units column? True
以上是 Python Pandas – 检查两个 DataFrame 的任何特定列是否相等 的全部内容, 来源链接: utcz.com/z/359123.html