如何比较两个 Pandas 数据框中的值?
让我们讨论如何比较 Pandas 数据框中的值。以下是比较两个熊猫数据框中的值的步骤:
步骤 1 数据框创建:可以使用以下代码创建两个数据集的数据框:
Python3
import pandas as pd
# elements of first dataset
first_Set = {'Prod_1': ['Laptop', 'Mobile Phone',
'Desktop', 'LED'],
'Price_1': [25000, 8000, 20000, 35000]
}
# creation of Dataframe 1
df1 = pd.DataFrame(first_Set, columns=['Prod_1', 'Price_1'])
print(df1)
# elements of second dataset
second_Set = {'Prod_2': ['Laptop', 'Mobile Phone',
'Desktop', 'LED'],
'Price_2': [25000, 10000, 15000, 30000]
}
# creation of Dataframe 2
df2 = pd.DataFrame(second_Set, columns=['Prod_2', 'Price_2'])
print(df2)
Python3
import numpy as np
# add the Price2 column from
# df2 to df1
df1['Price_2'] = df2['Price_2']
# create new column in df1 to
# check if prices match
df1['Price_Matching'] = np.where(df1['Price_1'] == df2['Price_2'],
'True', 'False')
df1
输出:
Step 2 数值比较:需要导入 numpy 才能成功执行这一步。这是执行比较的通用模板:
df1[‘new column for the comparison results’] = np.where(condition, ‘value if true’, ‘value if false’)
示例:执行此代码后,名称为Price_Matching的新列将在 df1 下形成。列结果将根据以下条件显示:
- 如果 Price_1 等于 Price_2,则赋值 True
- 否则,分配 False 的值。
Python3
import numpy as np
# add the Price2 column from
# df2 to df1
df1['Price_2'] = df2['Price_2']
# create new column in df1 to
# check if prices match
df1['Price_Matching'] = np.where(df1['Price_1'] == df2['Price_2'],
'True', 'False')
df1
输出: