比较python中每列的两个数据帧?
Df1: 我想比较子级别上每个卷号的卷号1001。 不知怎的,我创建了df2,它看起来像: df2 现在我想在多种条件下将每列与df1和df2进行比较比较python中每列的两个数据帧?,python,python-3.x,pandas,python-2.7,Python,Python 3.x,Pandas,Python 2.7,Df1: 我想比较子级别上每个卷号的卷号1001。 不知怎的,我创建了df2,它看起来像: df2 现在我想在多种条件下将每列与df1和df2进行比较 Roll_No Sub1 Sub2 Sub3 1001 40 50 60 1001 40 50 60 1001 40 50 60 1001 40 50 60 输出: Cond1 - df1.Sub1 -df2.Sub1 > 5 then 1 else 0 Cond2 - df
Roll_No Sub1 Sub2 Sub3
1001 40 50 60
1001 40 50 60
1001 40 50 60
1001 40 50 60
输出:
Cond1 - df1.Sub1 -df2.Sub1 > 5
then 1 else 0
Cond2 - df1.Sub2 -df2.Sub2 > 10
then 2 else 0
Cond3 - df1.Sub3 -df2.Sub3 > 7
then 3 else 0
来源
你的条件是什么?你的条件是什么?
Cond1 - df1.Sub1 -df2.Sub1 > 5
then 1 else 0
Cond2 - df1.Sub2 -df2.Sub2 > 10
then 2 else 0
Cond3 - df1.Sub3 -df2.Sub3 > 7
then 3 else 0
Roll_No Sub1 Sub2 Sub3
1001 0 0 0
1002 0 2 3
1003 0 0 3
1004 1 0 3
import pandas as pd
import numpy as np
Output = df1.copy()
Output['Sub1'] = np.where(df1['Sub1'] - df2['Sub1'] > 5, 1, 0)
Output['Sub2'] = np.where(df1['Sub2'] - df2['Sub2'] > 10, 2, 0)
Output['Sub3'] = np.where(df1['Sub3'] - df2['Sub3'] > 7, 3, 0)