简体   繁体   中英

Compare two data frames by each column in python?

Df1:

Roll_No Sub1 Sub2 Sub3
1001      40  50   60
1002      65  87   30
1003      36  49   40
1004      29  83   50

I want to compare Roll_No 1001 from every Roll_No on Sub Level. somehow I created df2 which looks like: df2

Roll_No Sub1 Sub2 Sub3
1001      40  50   60
1001      40  50   60
1001      40  50   60
1001      40  50   60

Now I want to compare each column with df1 & df2 on multiple condition.

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

Output:

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)

Source https://datatofish.com/compare-values-dataframes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM