Skip to content
Advertisement

Pandas multiple comparison on a single row

My source data looks like this:

JavaScript

enter image description here

I need to compare Second column with Third, Fourth Column with Fifth Column, Sixth with Seventh. Column names can change. So I have to consider the column positions and my first column with always has column name as id.

so if atleast one of comparisons (‘1_src1’ vs ‘1_src2’) ( ‘2_src1’ vs ‘2_src2’) fails if need to update 1 else 0. But if the comparison of (3_src1 vs 3_src2) fails if need to update 2 else 0.

My result should look like :

![enter image description here

The code I tried :

I tried creating subset of columns like this. But I am unable to proceed how can I achieve this result.

JavaScript

Any help appreciated. Thanks.

@Edit: Adding one more condition: Right now, if my last comparison fails I am updating ‘res’ as 2 but how can I update ‘res’ as 2 if my last n comparison fails?

Advertisement

Answer

You can use:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement